Can anyone help me in this:
Given the following database migration:
class DefineDatabase < ActiveRecord::Migration
def self.up create_table "products" do |t| t.column "name", :string t.column "price_converted", :integer # NOTE: we shouldn't store monetary values as floats to avoid rounding errors end
create_table "categories" do |t| t.column "name", :string end
create_table "categories_products" do |t| t.column "product_id", :integer, :null=>false t.column "category_id", :integer, :null=>false t.column "display_order", :integer, :default=>0, :null=>false end end
def self.down drop_table "products" drop_table "categories" drop_table "categories_products" end
end
I need to implement model classes named Category and Product so that the following code example generates the expected output:
cat1 = Category.new(:name=>'Books') cat1.save p1 = cat1.products.create(:name=>'My first book', :price=>10.99 ) puts p1.price_converted # should be the price in cents, that is x 100 and rounded up to next whole number >> 1099 p2 = cat1.products.create(:name=>'My second book', :price=>20.95 ) puts p2.price_converted >> 2095
cat1.set_display_order(p1, 1) # this should set the 'display_order' column associated with the product p1 to 1
prod_array = cat1.products.find(:all, :order=>'display_order, price') puts prod_array[0].name >> My second book puts prod_array[1].name