By inputting the initial data for operation check, If you reset the DB, you can recreate the initial data based on the seed file, Because you can create a large amount of data at once This is a necessary function for applications in the development stage.
This time, edit the seed file and input the initial data.
ruby 2.5.7 Rails 5.2.4.3 OS: macOS Catalina
The file created when the rails new 〇〇 command is executed is directly under the db directory. db/seeds.rb
db/seeds.rb
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
User.create!(
email: '[email protected]',
password: 'testpass',
)
Post.create!(
title: 'Thank you',
content: 'About seed files',
)
Terminal
$ rails db:seed
Now that the user can log in, the Post is populated with data. After that, as a confirmation method, execute as follows If the data is included, it is successful.
Terminal
$ rails c
pry(main)> Post.all
The for statement and the content are in, and the statement processing is repeated the specified number of times. The initial number starts from 0, so in the following cases it will be 0-9.
db/seeds.rb
Post.create!(
title: 'Thank you',
body: 'About seed files'
)
10.times do |number|
List.create!(title: 'About times',body: number)
end
There are other ways to input data from csv, so If you are interested, please check it out.
Also, on twitter, technologies and ideas that have not been uploaded to Qiita are also uploaded, so I would be grateful if you could follow me. Click here for details https://twitter.com/japwork
Recommended Posts