05. Seed files - max-borisov/ihub-bookshelf GitHub Wiki

Seed data are used to populate a database with initial data.

Main seeb.rb file is located under db folder.

db/seed.rb

OrderItem.destroy_all
Order.destroy_all
ShoppingCartItem.destroy_all
Review.destroy_all
Book.destroy_all
User.destroy_all

require_relative('../db/seeds/users')
require_relative('../db/seeds/books')
require_relative('../db/seeds/reviews')
require_relative('../db/seeds/shopping_cart_items')
require_relative('../db/seeds/orders')
require_relative('../db/seeds/order_items')

Lets create db/seeds folder with the following structure:

Seed data

db/seeds/users.rb

User.create!([
               {
                 name: 'Tom',
                 email: '[email protected]',
               },
               {
                 name: 'Jack',
                 email: '[email protected]',
                 admin: true
               }
])
puts("#{User.count} user(s) have been created.")

db/seeds/books.rb

amazon = ['1941222196', '1783981881', '1934356476', '0321984137', '1937785564', '1449373712', '1430223634', '1617291099', '1617291692', '1449321054', '1449373194', '144934013X', '1491901888', '1941222269']

amazon.each do |amazon_id|
  Book.create!({
                 title:        Faker::Name.title,
                 author:       Faker::App.author,
                 pub_date:     Faker::Date.between(rand(10..360).days.ago, Date.today),
                 description:  Faker::Lorem.paragraphs(rand(1..4)).map{ |i| "<p>#{i}</p>" }.join(''),
                 price:        Faker::Commerce.price,
                 isbn:         Faker::Code.isbn,
                 amazon_id:    amazon_id
  })
end
puts("#{Book.count} book(s) have been created.")

db/seeds/reviews.rb

tom = User.where(email: '[email protected]').first[:id]

rails = Book.where(amazon_id: 1941222196).first[:id]
yii2 = Book.where(amazon_id: 1783981881).first[:id]
ruby = Book.where(amazon_id: 1934356476).first[:id]

Review.create!([
                 {
                   text: Faker::Lorem.paragraphs(rand(2..6)).join(' '),
                   user_id: tom,
                   book_id: rails
                 },
                 {
                   text: Faker::Lorem.paragraphs(rand(2..6)).join(' '),
                   user_id: tom,
                   book_id: rails
                 },
                 {
                   text: Faker::Lorem.paragraphs(rand(2..6)).join(' '),
                   user_id: tom,
                   book_id: rails
                 },
                 {
                   text: Faker::Lorem.paragraphs(rand(2..6)).join(' '),
                   user_id: tom,
                   book_id: rails
                 },
                 {
                   text: Faker::Lorem.paragraphs(rand(2..6)).join(' '),
                   user_id: tom,
                   book_id: yii2
                 },
                 {
                   text: Faker::Lorem.paragraphs(rand(2..6)).join(' '),
                   user_id: tom,
                   book_id: ruby
                 },
                 {
                   text: Faker::Lorem.paragraphs(rand(2..6)).join(' '),
                   user_id: tom,
                   book_id: ruby
                 },
])
puts("#{Review.count} reviews(s) have been created.")

db/seeds/shopping_cart_items.rb

tom = User.where(email: '[email protected]').first[:id]
jack = User.where(email: '[email protected]').first[:id]

rails = Book.where(amazon_id: 1941222196).first[:id]
yii2 = Book.where(amazon_id: 1783981881).first[:id]
ruby = Book.where(amazon_id: 1934356476).first[:id]

ShoppingCartItem.create!([
               {
                 user_id: tom,
                 book_id: rails,
               },
               {
                 user_id: tom,
                 book_id: yii2,
               },
               {
                 user_id: tom,
                 book_id: ruby,
               }
])
puts("#{ShoppingCartItem.count} cart item(s) have been created.")

db/seeds/orders.rb

tom = User.where(email: '[email protected]').first[:id]

Order.create!([
               {
                 user_id: tom,
                 total_price: 299.89,
               },
               {
                 user_id: tom,
                 total_price: 150,
               }
])
puts("#{Order.count} order(s) have been created.")

db/seeds/order_items.rb

first_order = Order.first.id
last_order = Order.last.id

rails = Book.where(amazon_id: 1941222196).first[:id]
yii2 = Book.where(amazon_id: 1783981881).first[:id]
ruby = Book.where(amazon_id: 1934356476).first[:id]

OrderItem.create!([
                    {
                      order_id: first_order,
                      book_id: rails,
                    },
                    {
                      order_id: first_order,
                      book_id: yii2,
                    },
                    {
                      order_id: first_order,
                      book_id: ruby,
                    },
                    {
                      order_id: last_order,
                      book_id: ruby,
                    }
])
puts("#{OrderItem.count} order item(s) have been created.")

To fill in the database run the following command in the terminal:

$ rake db:seed

You should get smth. like that:

Seed result


Helpful links

⚠️ **GitHub.com Fallback** ⚠️