-Write the code in the project name /db/seeds.erb ・ This time, the user data of the repeating pattern of numbers is reflected in 50 db.
qiita.rb
if Rails.env == 'development'
(1..50).each do |i|
model name.create(name: "user#{i}", title: "title#{i}", body:"Text#{i}")
end
end
[Explanation]
Rails.env == 'development'
-Determine whether the rails execution environment is in development mode
・ There are three types of env: "development", "test", and "production".
model name.cretate (column name: value, column name: value)
· Code that stores the value in the database
・ Model name
should be described in the singular system.
[About create method] It is possible to describe vertically by doing as follows
qiita.rb
model name.create([
{ name: 'Value 1' },
{ name: 'Value 2' },
{ name: 'Value 3' },
{ name: 'Value 4' },
{ name: 'Value 5' },
{ name: 'Value 6' }
])
end
[Explanation] -Write in hash format in the array ・ Multiple data can be saved together
rails db:seed
-Code to reflect the information described in seeds.rb in db
Recommended Posts