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
>> My first book
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---