Forum: Ruby on Rails Rails Migration Question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Anil B. (Guest)
on 2008-12-29 12:24
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
MaD (Guest)
on 2008-12-29 12:34
(Received via mailing list)
instead of just looking for someone that is willing to implement your
models and methods, why don't you just give it a try first and ask if
you encounter any problems?

start by defining the relations between models (has_many,
belongs_to, ...). add callbacks (after_save :convert_price) and
methods (def convert_price). all along write some tests to see if your
code is working correctly.

if you're stuck somewhere, come back and ask for help.
This topic is locked and can not be replied to.