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.
A function that allows you to easily flow initial data after creating a database.
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.
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.
--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
Rails Guide v6.0 https://railsguides.jp/active_record_migrations.html
Recommended Posts