A note about the seed function of Ruby on Rails

Introduction

I'm a beginner. I'm making an application using Ruby and Ruby on Rails. It also serves as a memorandum, so please point out any mistakes.

What is the seed function?

A function that allows you to easily flow initial data after creating a database.

Which file to use

Write the code you want to flow as initial data in db / seeds.rb, You can do rails db: seed in the terminal. Even if you execute it, nothing will be displayed in the terminal, but if there is no problem with the data, it should be filled.

How to write in the file

Example) Fill 5 data into the name column and description column of the products table

5.times do |i|
  Product.create(name: "Product ##{i}", description: "A product.")
end

I'm using the times method, but there's no problem writing one line at a time.

Summary

--The seed function is a function that allows you to flow initial data after creating a database. --Describe the data you want to flow into db / seeds.rb --You can pour it by doing rails db: seed in the terminal

reference

Rails Guide v6.0 https://railsguides.jp/active_record_migrations.html

Recommended Posts

A note about the seed function of Ruby on Rails
[Note] About the Fizz_Buzz problem (How Ruby on Rails works)
[Rails] Addition of Ruby On Rails comment function
part of the syntax of ruby ​​on rails
[Ruby on rails] Implementation of like function
(Ruby on Rails6) Create a function to edit the posted content
Implementation of Ruby on Rails login function (Session)
[Ruby on Rails] A memorandum of layout templates
A note about the Rails and Vue process
A note about the scope
[Ruby on Rails] about has_secure_password
[Ruby on Rails] Asynchronous communication of posting function, ajax
Implementation of Ruby on Rails login function (devise edition)
Docker the development environment of Ruby on Rails project
[Ruby on Rails] Implementation of tagging function/tag filtering function
Try using the query attribute of Ruby on Rails
[Ruby on Rails] Implement a pie chart that specifies the percentage of colors
[Rails] About the Punk List function
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Note on the path of request.getRequestDispatcher
(Ruby on Rails6) Display of the database that got the id of the database
[Ruby on Rails] CSV output function
Delete all the contents of the list page [Ruby on Rails]
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
I want to add a browsing function with ruby on rails
[Rails] How to put a crown mark on the ranking function
[Illustration] Finding the sum of coins with a recursive function [Ruby]
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Search function (not selected)
Make a note of Ruby keyword arguments
[Swift] A note about function and closure
A note on the libGDX Utils class
Let's summarize "MVC" of Ruby on Rails
[Ruby on Rails] About bundler (for beginners)
[Ruby on Rails] About Active Record callbacks
[Ruby on Rails] Japanese notation of errors
Explanation of Ruby on rails for beginners ①
A rudimentary note on the Fibonacci sequence
[Ruby on Rails] Logical deletion (withdrawal function)
Create a large number of records with one command using the Ruby on Rails seeds.rb file
How to solve the local environment construction of Ruby on Rails (MAC)!
Ruby on Rails When you don't know the cause of rollback when saving.
A series of flow of table creation → record creation, deletion → table deletion in Ruby on Rails
[Ruby On Rails] How to search the contents of params using include?
[Ruby On Rails] About RubyAws :: Sigv4 :: Errors :: MissingCredentialsError
When the Ruby on Rails terminal rolls back
Recommendation of Service class in Ruby on Rails
Ruby on Rails ~ Basics of MVC and Router ~
Introducing Rspec, a Ruby on Rails test framework
Ruby on Rails Email automatic sending function implementation
(Ruby on Rails6) Creating data in a table
[Note] Summary of rails login function using devise ①
Determine the current page with Ruby on Rails
[Ruby on Rails] Post editing function (update, delete)
[Ruby on Rails] Individual display of error messages
I made a portfolio with Ruby On Rails
[Ruby on Rails] How to make the link destination part of the specified id
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed