Create an EC site with Rails5 ⑥ ~ seed data input ~

Introduction

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

Source code

https://github.com/Sn16799/bakeryFUMIZUKI

Manage multiple seed files

It's just data to check if the display can be done correctly, so I think you should make it at a minimum. However, since there are many Models, the amount of description will be enormous if you try to do everything with 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 one seeds.rb file, click here](#Create all data))

$ 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

Make it possible to read the created files from the seeds.rb file. Now when you hit rails db: seed, the data in the seeds folder will also be recognized. The point to note here is ** write the parent Model above **. Since the files are read in order from the top line, if the child Model comes first, an error "No Customer data!" Will occur.

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 the Admin data here. One administrator's data is enough, and the amount of code is not enough to divide it into separate files.

Make a lot of data

架空の人名架空の地名はいつものサイトで調達します。

Address

db/seeds/.rb


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

    {
      id: 2,
      customer_id: 2,
      addressee: 'Yumiko Suzuka',
      post_code: '2222222',
      address: '3 Shiromi City, Hosono Prefecture-1-7',
    },

    {
      id: 3,
      customer_id: 3,
      addressee: 'Chiemi Sekizawa',
      post_code: '3333333',
      address: '6 Ochicho, Hatsuyama City, Akada Prefecture-12',
    },

    {
      id: 4,
      customer_id: 4,
      addressee: 'Volume transfer',
      post_code: '4444444',
      address: '8 Gomedai, Hashima City, Neno Prefecture-12',
    },

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

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 Majoji Temple, Otonishikawa City, Kitaoka Prefecture-6-1',
      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 Masuokigaoka, Kaminoyama City, Torikawa Prefecture-15',
      tel: '22222222',
      is_active: true,
      password: '222222',
      password_confirmation: '222222',
    },

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

    {
      id: 4,
      email: '[email protected]',
      family_name: 'Tsukagoshi', family_name_kana: 'Tsukagoshi',
      first_name: 'Kimi',   first_name_kana: 'Takami',
      post_code: '4444444',
      address: '5 Shibayashigawa Town, Fuyuta City, Kasakawa Prefecture-2-7',
      tel: '44444444',
      is_active: true,
      password: '444444',
      password_confirmation: '444444',
    },

    {
      id: 5,
      email: '[email protected]',
      family_name: 'Kamada', family_name_kana: 'Kamata',
      first_name: 'Yukihiro',   first_name_kana: 'Yukihiro',
      post_code: '5555555',
      address: 'First 2 Oaza Wei, Hibikiyama City, Hayada Prefecture-16',
      tel: '55555555',
      is_active: true,
      password: '555555',
      password_confirmation: '555555',
    },

  ]
  )

Genre

db/seeds/.rb


Genre.create!(
  [
    {
      id: 1,
      name: 'Plain bread',
      validity: true
    },

    {
      id: 2,
      name: 'Side dish 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 Inashige',
      post_code: '1111111',
      send_to_address: '2 Shinanji Temple, Iwaoyama Town, Tsukikawa Prefecture-15',
      how_to_pay: true,
      order_status: 0,
    },

    {
      id: 2,
      customer_id: 2,
      addressee: 'Yumiko Suzuka',
      post_code: '2222222',
      send_to_address: '3 Shiromi City, Hosono Prefecture-1-7',
      how_to_pay: true,
      order_status: 2,
    },

    {
      id: 3,
      customer_id: 3,
      addressee: 'Chiemi Sekizawa',
      post_code: '3333333',
      send_to_address: '6 Ochicho, Hatsuyama City, Akada Prefecture-12',
      how_to_pay: true,
      order_status: 1,
    },

    {
      id: 4,
      customer_id: 4,
      addressee: 'Volume transfer',
      post_code: '4444444',
      send_to_address: '8 Gomedai, Hashima City, Neno Prefecture-12',
      how_to_pay: false,
      order_status: 2,
    },

    {
      id: 5,
      customer_id: 5,
      addressee: 'Yayoi Okazaki',
      post_code: '5555555',
      send_to_address: '3 Karuiyama, Shiwa City, Furuoka Prefecture-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: 'Brioche',
      introduction: 'It is a bread with a rich butter flavor.',
      genre_id: 1,
      price: 500,
      status: true,
    },

    {
      id: 2,
      name: 'baguette',
      introduction: 'Domestic wheat 100%French bread.',
      genre_id: 1,
      price: 260,
      status: true,
    },

    {
      id: 3,
      name: 'Curry bread',
      introduction: 'Medium spicy curry is combined with sweet bread dough.',
      genre_id: 2,
      price: 180,
      status: true,
    },

    {
      id: 4,
      name: 'Hamburger',
      introduction: 'I put a lot of ingredients in homemade buns.',
      genre_id: 2,
      price: 370,
      status: true,
    },

    {
      id: 5,
      name: 'anpan',
      introduction: 'It is a novel anpan that is delicious no matter where you eat it.',
      genre_id: 3,
      price: 160,
      status: true,
    },

    {
      id: 6,
      name: 'Cream bun',
      introduction: 'Please enjoy our special custard.',
      genre_id: 3,
      price: 170,
      status: true,
    },

    {
      id: 7,
      name: 'Pain au chocolat',
      introduction: 'Our most popular ♪',
      genre_id: 3,
      price: 230,
      status: true,
    },

    {
      id: 8,
      name: 'Laugen Brothchen',
      introduction: 'German-style chewy bread.',
      genre_id: 4,
      price: 200,
      status: true,
    },


    {
      id: 9,
      name: 'Strawberry flower bread',
      introduction: 'Strawberry flavor is added to the fluffy dough.',
      genre_id: 4,
      price: 180,
      status: true,
    },

    {
      id: 10,
      name: 'Mont Blanc',
      introduction: 'Please combine marron cream with brioche.',
      genre_id: 5,
      price: 270,
      status: true,
    },

    {
      id: 11,
      name: 'pancake',
      introduction: 'A moist pancake made from strong flour.',
      genre_id: 5,
      price: 150,
      status: true,
    },
  ]
  )

Create data together

If you are not particular about the appearance and you don't mind using data such as "Fumizuki Taro 1" and "Fumizuki Taro 2", you can create multiple data at once.

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: "July#{n}",
                   family_name_kana: "Fumizuki#{n}",
                   first_name: "Taro#{n}",
                   first_name_kana: "Taro#{n}",
                   post_code: "1111111",
                   address: "Emukai-ji Temple, Hikawa City, Yokooka Prefecture#{n}Chome",
                   tel: "11111111111",
                   is_active: true,
                   password: "111111",
                   password_confirmation: "111111",
                   )

  Address.create!(
                  customer_id: 1,
                  addressee: "Nana Fumizuki#{n}",
                  post_code: '1111111',
                  address: '2 Matsubayashi, Higashi Satomi Town, Mihashi Prefecture-15-#{n}'
                   )

  Genre.create!(
                name: "Genre that#{n}",
                validity: true
                )

  Product.create!(
                  name: "Homemade bread#{n}",
                  introduction: "#{n}It's twice as delicious! (Compared to our company)",
                  genre_id: 1,
                  price: 260,
                  status: true,

  Order.create!(
                customer_id: 1,
                addressee: "Fumizuki Hanako#{n}",
                post_code: "1111111",
                send_to_address: "Torikubo Town, Yumei Prefecture",
                how_to_pay: true,
                order_status: 1,
               )

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

end

Postscript

I'm not sure when it is appropriate to put the seed data, but I put it in for the time being. It may be the smoothest to make all the steps after setting the Model. Even if there is no data, the app itself can be created, so I'm at a loss.

When developing an application, I can't decide in what order it is most efficient, and I feel that I'm doing it in a rather messy order. After finishing this EC site, I would like to change the order of the commentary articles and put together a tutorial that can be implemented crisply.

The biggest question is "whether or not it can be completed"! Continue to next time!

reference

Various ways to write rails seed Separate and manage Rails / seed files

Recommended Posts

Create an EC site with Rails5 ⑥ ~ seed data input ~
Create an EC site with Rails5 ⑤ ~ Customer model ~
Create an EC site with Rails 5 ⑩ ~ Create an order function ~
Create an EC site with Rails5 ⑦ ~ Address, Genre model ~
Create an EC site with Rails 5 ⑨ ~ Create a cart function ~
Create an EC site with Rails5 ④ ~ Header and footer ~
Create an EC site with Rails5 ② ~ Bootstrap4 settings, Controller / action definition ~
Create an EC site with Rails5 ③-Set model associations and other things-
[Rails] Initial data creation with seed
Initial data input with [Rails] seed_fu!
Create an EC site using stripe! (Account creation)
[Rails] Create an application
Rails6: Input the initial data of ActionText using seed
Creating an EC site with Rails5 ①-App configuration, various gem preparation, Model / Routing creation-
[Rails] EC site cart function
Create an immutable class with JAVA
Create portfolio with rails + postgres sql
Create an app with Spring Boot 2
Create an app catalog site using CLI for Microsoft 365 with Docker
[Rails] DB design for EC site
Create pagination function with Rails Kaminari
Create an excel file with poi
Create an app with Spring Boot
Create My Page with Rails devise
[Rails 6] Two methods to import multiple images at once using CarrierWave / (1) Input with initial seed data / (2) Import with CSV
[Java] Create an executable module with Gradle
[Rails6] Create a new app with Rails [Beginner]
Create Rails 6 + MySQL environment with Docker compose
[Rails withdrawal] Create a simple withdrawal function with rails
[Rails] Manage data for each seed file
[Rails] Get standard input for multi-line data
[Rails 5] Create a new app with Rails [Beginner]
Let's make an error screen with Rails
Creating an EC site with Rails5 ⑧ ~ Product model edition, narrowed down display with conditions for both parents and children ~
Nuxt.js × Create an application in Rails API mode
Downgrade an existing app created with rails 5.2.4 to 5.1.6
[Rails] rails new to create a database with PostgreSQL
Create an RSA encryption-enabled JSON API with wicket
Create an app by specifying the Rails version
Rails6.0 ~ How to create an eco-friendly development environment
Create an Annotator that uses kuromoji with NLP4J [007]
[Rails] How to build an environment with Docker
Create dummy data of portfolio with Faker [Note]