Ruby 2.6.5 Rails 6.0.3
seed.rb
User.create!(
email: "[email protected]",
name: "user1",
passowrd: "password"
)
Create! Action for the model you want to create a record for. I will give the value of each column.
seed.rb
users = User.create!([
{nickname: "guest", email: "[email protected]", password: "guest1234"},
{nickname: "guest2", email: "[email protected]", password: "guest5678"},
])
Prepare an array. It can be created by passing record data in the form of a hash to each element of the array.
seed.rb
20.times do |n|
User.create!(
name: "guest#{n+1}",
email: "guest#{n+1}@guest.com",
password: "guest#{n+1}0000",
)
end
Execute the create! Action multiple times using the times method. Implement different values in each record by including n in the value so that columns with uniqueness do not overlap.
seed.rb
stations = ["Tokyo",
"Yurakucho",
"Shimbashi",
"Hamamatsucho",
"Tamachi",
"Takanawa Gateway",
"Shinagawa",
"Osaki" ,
"Gotanda",
"Meguro",
"Ebisu",
"Shibuya",
"Harajuku",
"Yoyogi",
"Shinjuku",
"Shin-Okubo",
"Takadanobaba",
"Mejiro",
"Ikebukuro",
"Otsuka",
"Sugamo",
"Komagome",
"Tabata",
"Nishinippori",
"Nippori",
"Uguisudani" ,
"Ueno",
"Okachimachi",
"Akihabara",
"Kanda"]
stations.length.times do |i|
Station.create!(
name: stations[i]
)
end
After preparing the array, iterate as many times as there are, and give the value of the array to the column. (I think I should be able to go with each, but for some reason it didn't work and I processed it with times) I used an array because I wanted to reuse this value for another column. (Specifically, when I made the demo data of the restaurant after this, I wanted to include the station name in the store name, so I chose this method)
seed.rb
Item.create!(
name: "sofa",
produce: "Brand new",
price: 8000,
images_attributes: [{image: File.open('./app/assets/images/index_item01.png',)}]
)
When the Item table nests the Image table and has a has_many relationship. Since images_attributes has multiple record data, create an array and give the record data of the Image table by hash in it.
seed.rb
10.times do |i|
Shop.create!(
name: "Fresh fish shop#{stations[i]}shop",
address: addresses[i],
capacity: 40,
mainimage: File.open('./app/assets/images/shops/shop1_1.jpg'),
maincontent: "We are waiting for you to prepare seafood directly from the production area!",
likepoints: 0,
station_ids: [i+1,i+2],
introduces_attributes: [
{subcontent: "You can enjoy your meal in the calm interior.",
number: 1,
subimage: File.open('./app/assets/images/shops/shop1_2.jpg')},
{subcontent:"We recommend the seafood platter!",
number: 2,
subimage: File.open('./app/assets/images/shops/shop1_3.jpg')},
{subcontent: "A talkative clerk is waiting for you!",
number: 3,
subimage: File.open('./app/assets/images/shops/shop1_4.jpg')}
]
)
end
Multiple shop data are created using the array called stations. The shop table nests the introduces table, and I want to pass multiple records to the introduces table, so I pass an array in the form of a hash. It's not that complicated, surprisingly. (I don't know the cause, but this didn't work at first, and when I adjusted the indentation, something went wrong.
When I studied how the data was sent in the form, I understood a lot, so I didn't have to worry too much about the description in the seed file. While reading various things, I got the impression that the phrase "the array is taken out as a hash and the hash is taken out as an array to save the data" that came out in the official text was very easy to understand (remember).
I will leave it as a reminder.
Recommended Posts