Commit 0531803a by Hoang Phuc Do

Populating products and categories default data

parent faf4ff28
......@@ -58,4 +58,4 @@ group :development do
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
\ No newline at end of file
class Category < ApplicationRecord
has_many :products, dependent: :destroy
end
\ No newline at end of file
class Product < ApplicationRecord
belongs_to :category
end
\ No newline at end of file
......@@ -5,3 +5,27 @@
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
# Create Product Categories
5.times do |n|
freq = n + 1
title = "Category #{freq}"
desc = "Description for category #{freq}"
Category.create!(title: title, description: desc)
end
# Create Products
categories = Category.take(5)
40.times do |n|
freq = n + 1
title = "Product #{freq}"
desc = "Description for product #{freq}"
categories.each do |cat|
sku = "u-#{rand(1..999)}"
price = (200.0 - 5.0) * rand + 5
cat.products.create!(title: title,
description: desc,
sku: sku,
price: price)
end
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment