[Ruby] Creating an EC site with Rails 5 ⑥-Seeding seed data-

4 minute read

Introduction

This is a continuation of the series Create an EC site with Rails 5 ⑤, a series for creating an EC site where you can shop at a fictitious bakery. In continuing the implementation after this, it seems convenient to have the initial data, so I decided to give priority to that.

Source code

https://github.com/Sn16799/bakeryFUMIZUKI

Manage multiple seed files

Since it is data to confirm whether the display can be correctly done, I think that it is good to make it at a minimum. However, since the number of Models is large, the amount of description will be enormous if you try to complete all in one file. Therefore, I decided to create a new folder called seeds and put the initial data for each Model in it. (If you want to use only one seeds.rb file, here)

$ cd work/fumizuki/db
$ mkdir seeds
$ cd seeds
$ touch customer.rb
$ touch address.rb
$ touch genre.rb
$ touch product.rb
$ touch order.rb
$ touch order_item.rb

Read files, create Admin data

Allow the created files to be read from the seeds.rb file. Now when you hit rails db:seed, the data in the seeds folder will be recognized. The important point here is to describe the parent model above. The file is read in order from the above line, so if the child Model is set first, an error will occur saying “There is no Customer data!”.

db/seeds.rb


require'./db/seeds/customer.rb'
require'./db/seeds/address.rb'
require'./db/seeds/genre.rb'
require'./db/seeds/product.rb'
require'./db/seeds/order.rb'
require'./db/seeds/order_item.rb'

Admin.create!(
  id: 1,
  email:'[email protected]',
  password:'adminuser',
  password_confirmation:'adminuser'
  )

I also wrote Admin data here. One administrator’s data is enough, and it’s not enough code to separate files.

Make a lot of data

Fictitious person nameandFictitiousplacename are procured at the usual site. To do.

Address

db/seeds/.rb


Address.create!(
  [
    {
      id: 1,
      customer_id: 1,
      addressee:'Ayako Inatsugi',
      post_code: '1111111',
      address: '2-15 Shinanji, Iwaoyama-cho, Tsukikawa Prefecture',
    },

    {
      id: 2,
      customer_id: 2,
      addressee:'Yumiko Suzuka',
      post_code: '2222222',
      address: '3-1-7 Seshigaoka, Jomi-shi, Hosono',
    },

    {
      id: 3,
      customer_id: 3,
      addressee:'Chie Sekizawa',
      post_code: '3333333',
      address: '6-12 Tokoshi-cho, Hatsuyama-shi, Akada',
    },

    {
      id: 4,
      customer_id: 4,
      addressee:'Transfer',
      post_code: '4444444',
      address: '8-12 Gomedai, Hashima-shi, Neno',
    },

    {
      id: 5,
      customer_id: 5,
      addressee:'Yayoi Okazaki',
      post_code: '5555555',
      address:'3-5-2 Karuiyama, Shiwa-shi, Furuoka',
    },
  ]
  )

Customer

db/seeds/.rb


Customer.create!(
  [
    {
      id: 1,
      email: '[email protected]',
      family_name:'Nagaoka', family_name_kana:'Nagaoka',
      first_name:'Satomi', first_name_kana:'Satomi',
      post_code: '1111111',
      address: '2-6-1 Majoji, Otonishikawa, Kitaoka',
      tel: '11111111',
      is_active: true,
      password: '111111',
      password_confirmation: '111111',
    },

    {
      id: 2,
      email: '[email protected]',
      family_name:'Nozaki', family_name_kana:'Nozaki',
      first_name:'Kengo', first_name_kana:'Kengo',
      post_code: '2222222',
      address: '1-15 Makiokigaoka, Banjoyama, Torikawa',
      tel: '22222222',
      is_active: true,
      password: '222222',
      password_confirmation: '222222',
    },

    {
      id: 3,
      email: '[email protected]',
      family_name:'Seno', family_name_kana:'Theo',
      first_name:'Chiyomi', first_name_kana:'Chiyomi',
      post_code: '3333333',
      address: '3-2 Yamamaki Gakuen, Nakawa-cho, Nita',
      tel: '33333333',
      is_active: true,
      password: '333333',
      password_confirmation: '333333',
    },

    {
      id: 4,
      email: '[email protected]',
      family_name:'Tsukagoe', family_name_kana:'Tsukagoshi',
      first_name:'Kimi', first_name_kana: "Takami",
      post_code: '4444444',
      address: '5-2-7 Shirinkawa-cho, Fuyuta-shi, Kasakawa',
      tel: '44444444',
      is_active: true,
      password: '444444',
      password_confirmation: '444444',
    },

    {
      id: 5,
      email: '[email protected]',
      family_name:'Kamata', family_name_kana:'Kamata',
      first_name:'Yukihiro', first_name_kana:'Yukihiro',
      post_code: '5555555',
      address: '2-16 Weihaji, Kyoyama-shi, Waseda',
      tel: '55555555',
      is_active: true,
      password: '555555',
      password_confirmation: '555555',
    },

  ]
  )

Genre

db/seeds/.rb


Genre.create!(
  [
    {
      id: 1,
      name: "Bread",
      validity: true
    },

    {
      id: 2,
      name: "Café bread",
      validity: true
    },

    {
      id: 3,
      name: "Sweet bread",
      validity: true
    },

    {
      id: 4,
      name:'Limited',
      validity: true
    },

    {
      id: 5,
      name:'Other sweets',
      validity: true
    },
  ]
  )

Order

db/seeds/.rb


Order.create!(
  [
    {
      id: 1,
      customer_id: 1,
      addressee:'Ayako Inatsugi',
      post_code: '1111111',
      send_to_address: '2-15 Shinanji, Iwaaoyama-cho, Tsukikawa Prefecture',
      how_to_pay: true,
      order_status: 0,
    },

    {
      id: 2,
      customer_id: 2,
      addressee:'Yumiko Suzuka',
      post_code: '2222222',
      send_to_address: '3-1-7 Seshigaoka, Jonami-shi, Hosono',
      how_to_pay: true,
      order_status: 2,
    },

    {
      id: 3,
      customer_id: 3,
      addressee:'Chie Sekizawa',
      post_code: '3333333',
      send_to_address: '6-12 Tokoshi-cho, Hatsuyama-shi, Akada',
      how_to_pay: true,
      order_status: 1,
    },

    {
      id: 4,
      customer_id: 4,
      addressee:'Transfer',
      post_code: '4444444',
      send_to_address: '8-12 Gomedai, Hashima-shi, Neno',how_to_pay: false,
      order_status: 2,
    },

    {
      id: 5,
      customer_id: 5,
      addressee: '岡崎弥生',
      post_code: '5555555',
      send_to_address: '古岡県紫波市刈唯山3-5-2',
      how_to_pay: true,
      order_status: 0,
    },
  ]
  )

OrderItem

db/seeds/.rb


OrderItem.create!(
  [
    {
      id: 1,
      order_id: 1,
      product_id: 10,
      quantity: 4,
      order_price: 270,
      make_status: 2,
    },

    {
      id: 2,
      order_id: 1,
      product_id: 7,
      quantity: 6,
      order_price: 230,
      make_status: 1,
    },

    {
      id: 3,
      order_id: 2,
      product_id: 2,
      quantity: 2,
      order_price: 260,
      make_status: 0,
    },

    {
      id: 4,
      order_id: 3,
      product_id: 3,
      quantity: 6,
      order_price: 180,
      make_status: 1,
    },

    {
      id: 5,
      order_id: 4,
      product_id: 4,
      quantity: 8,
      order_price: 370,
      make_status: 1,
    },

    {
      id: 6,
      order_id: 5,
      product_id: 5,
      quantity: 10,
      order_price: 160,
      make_status: 2,
    },
  ]
  )

Product

db/seeds/.rb


Product.create!(
  [
    {
      id: 1,
      name: 'ブリオッシュ',
      introduction: 'バターの風味豊かな食パンです。',
      genre_id: 1,
      price: 500,
      status: true,
    },

    {
      id: 2,
      name: 'バゲット',
      introduction: '国産小麦100%のフランスパン。',
      genre_id: 1,
      price: 260,
      status: true,
    },

    {
      id: 3,
      name: 'カレーパン',
      introduction: '中辛のカレーを、甘いパン生地と組み合わせました。',
      genre_id: 2,
      price: 180,
      status: true,
    },

    {
      id: 4,
      name: 'ハンバーガー',
      introduction: '自家製のバンズに具材をたくさん挟み込みました。',
      genre_id: 2,
      price: 370,
      status: true,
    },

    {
      id: 5,
      name: 'あんぱん',
      introduction: 'どこから食べても美味しい斬新なあんぱんです。',
      genre_id: 3,
      price: 160,
      status: true,
    },

    {
      id: 6,
      name: 'クリームパン',
      introduction: '当店こだわりのカスタードをお楽しみください。',
      genre_id: 3,
      price: 170,
      status: true,
    },

    {
      id: 7,
      name: 'パン・オ・ショコラ',
      introduction: '当店の一番人気です♪',
      genre_id: 3,
      price: 230,
      status: true,
    },

    {
      id: 8,
      name: 'ラウゲン・ブロートヒェン',
      introduction: 'ドイツ風のもっちりパン。',
      genre_id: 4,
      price: 200,
      status: true,
    },


    {
      id: 9,
      name: 'いちごのお花パン',
      introduction: 'ふんわりとした生地に苺の風味を加えました。',
      genre_id: 4,
      price: 180,
      status: true,
    },

    {
      id: 10,
      name: 'モンブラン',
      introduction: 'ブリオッシュにマロンクリームを合わせてどうぞ。',
      genre_id: 5,
      price: 270,
      status: true,
    },

    {
      id: 11,
      name: 'パンケーキ',
      introduction: '強力粉からつくるしっとりしたパンケーキです。',
      genre_id: 5,
      price: 150,
      status: true,
    },
  ]
  )

データをまとめて作成

もし見た目にこだわりがなく、「文月太郎1」「文月太郎2」のようなデータで構わない場合は、複数のデータを一気に作ることもできます。

db/seeds.rb


Admin.create!(
  id: 1,
  email: '[email protected]',
  password: 'adminuser',
  password_confirmation: 'adminuser'
  )

50.times do |n|
  Customer.create!(
                   email: "[email protected]#{n}",
                   family_name: "文月#{n}",
                   family_name_kana: "フミヅキ#{n}",
                   first_name: "太郎#{n}",
                   first_name_kana: "タロウ#{n}",
                   post_code: "1111111",
                   address: "横岡県氷川市絵向寺#{n}丁目",
                   tel: "11111111111",
                   is_active: true,
                   password: "111111",
                   password_confirmation: "111111",
                   )

  Address.create!(
                  customer_id: 1,
                  addressee: "文月なな#{n}",
                  post_code: '1111111',
                  address: '三橋県東里見町松林2-15-#{n}'
                   )

  Genre.create!(
                name: "ジャンルその#{n}",
                validity: true
                )

  Product.create!(
                  name: "自家製パン#{n}",
                  introduction: "#{n}倍美味しくなりました!(当社比)",
                  genre_id: 1,
                  price: 260,
                  status: true,

  Order.create!(
                customer_id: 1,
                addressee: "文月花子#{n}",
                post_code: "1111111",
                send_to_address: "遊明県鳥窪町",
                how_to_pay: true,
                order_status: 1,
               )

  OrderItem.create!(
                    order_id: 1,
                    product_id: 5,
                    quantity: 10,
                    order_price: 600,
                    make_status: 1,
                   )

end

後記

seedデータはいつ入れるのが適切なのか、よく分かりませんがとりあえず入れました。Modelの設定を終えた段階で一通り作ってしまうのが、一番スムーズかも知れません。データが入っていなくてもアプリ自体は作れるので、余計に迷うところです。

アプリ開発をする上で、どのような順序で進めるのが最も効率的なのか判断しきれずに、かなりめちゃくちゃな順番でやっているのではという気がしています。このECサイトを仕上げたら、解説記事たちも順番を入れ替えて、サクサク実装できるチュートリアルにまとめたいと思います。

一番の問題は「完成までこぎつけるかどうか」ですけどね! 次回へ続く!

参考

railsのseedの書き方いろいろRails/seedfilesaredividedandmanaged