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.
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:

User.create!([
               {
                 name: 'Tom',
                 email: '[email protected]',
               },
               {
                 name: 'Jack',
                 email: '[email protected]',
                 admin: true
               }
])
puts("#{User.count} user(s) have been created.")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.")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.")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.")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.")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:seedYou should get smth. like that:
