[Ruby] [Rails] Various ways to describe in seed file

2 minute read

*This is a reminder to yourself. If you understand how to create data in the basic form, I think there are many things that give you an image.

Environment

Ruby 2.6.5 Rails 6.0.3

How to write

① Create only one (basic)

seed.rb


User.create!(
   email: "[email protected]ser.com",
   name: "user1",
   passowrd: "password"
)

Create! action for the model you want to make a record. I will give the value of each column.

② Create multiple demo data

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 hash to each element of the array.

③ Create multiple demo data (repeated processing)

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 create! action multiple times using times method. Implemented different values in each record by including n in the value so that columns with uniqueness do not overlap.

④ Combine arrays

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",
  "Nishi-nippori",
  "Nippori",
  "Uguisudani",
  "Ueno",
  "Okachimachi",
  "Akihabara",
  "Kanda"]

stations.length.times do |i|
  Station.create!(
    name: stations[i]
  )
end

After preparing the array, iterate the number of times and give the array value 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 demo data for restaurants after that, I wanted to include the station name in the store name, so I chose this method.)

⑤ Create a record that includes the attributes nest

seed.rb


Item.create!(
  name: "Sofa",
  produce: "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.

⑥ Various combinations up to now

seed.rb


10.times do |i|
  Shop.create!(
    name: "Fresh fish store #{stations[i]} store",
    address: addresses[i],
    capacity: 40,
    mainimage: File.open('./app/assets/images/shops/shop1_1.jpg'),
    maincontent: "We are waiting for seafood directly from the production area!",
    likepoints: 0,
    station_ids: [i+1,i+2],
    introduces_attributes: [
      {subcontent: "You can enjoy your meal in a calm atmosphere.",
        number: 1,
        subimage: File.open('./app/assets/images/shops/shop1_2.jpg')},
      {subcontent: "Recommended seafood platter!",
        number: 2,
        subimage: File.open('./app/assets/images/shops/shop1_3.jpg')},
      {subcontent: "A clerk who likes talking is waiting for you!",
        number: 3,
        subimage: File.open('./app/assets/images/shops/shop1_4.jpg')}
    ]
  )
end

I’m using the array called stations to create multiple shop data. The shop table nests the introduces table, and since we want to pass multiple records to the introduces table, we are passing an array in the form of a hash. Unexpectedly not that complicated. (The cause is unknown, but this didn’t work at first, and if I did indentation, it passed. What was it.)

Summary

I didn’t bother to write it in the seed file because I understood a lot when I learned how to send data in the form. While reading variously, it was an impression that the word “array is fetched by hash, and hash is used to save data by fetching array” that came out in the official or some text was very easy to understand (remembered)

I’ll leave it as a reminder.