Repository navigation
Expand file tree
/
Copy pathseeds.rb
More file actions
49 lines (43 loc) · 1.34 KB
/
Copy pathseeds.rb
File metadata and controls
49 lines (43 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
LineItem.destroy_all
Order.destroy_all
Customer.destroy_all
Product.destroy_all
20.times do
name = "#{Faker::Name.first_name} #{Faker::Name.last_name}"
Customer.create(
name: name,
email: Faker::Internet.free_email(name),
)
end
product_attributes = YAML.load_file("./db/seeds/products.yml")
product_attributes.each do |attributes|
Product.create attributes.merge(price: 20 + rand(50))
end
Customer.all.each do |customer|
(1..3).to_a.sample.times do
order = Order.create(
customer: customer,
address_line_one: Faker::Address.street_address,
address_line_two: Faker::Address.secondary_address,
address_city: Faker::Address.city,
address_state: Faker::Address.state_abbr,
address_zip: Faker::Address.zip,
)
item_count = (1..3).to_a.sample
Product.all.sample(item_count).each do |product|
LineItem.create(
order: order,
product: product,
unit_price: product.price,
quantity: (1..3).to_a.sample,
)
end
end
end