# Rails Migration Question

**URL:** <https://www.ruby-forum.com/t/rails-migration-question/156518>\
**Category:** Rails\
**Created:** [December 29, 2008, 11:24am UTC](https://www.ruby-forum.com/t/rails-migration-question/156518 "2008-12-29T11:24:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anilbhat21](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/77aa72/32.png) [@anilbhat21](https://www.ruby-forum.com/u/anilbhat21)\
**Post date:** [December 29, 2008, 11:24am UTC](https://www.ruby-forum.com/t/rails-migration-question/156518/1 "2008-12-29T11:24:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anilbhat21](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/77aa72/32.png) [@anilbhat21](https://www.ruby-forum.com/u/anilbhat21)\
**Post date:** [December 29, 2008, 11:34am UTC](https://www.ruby-forum.com/t/rails-migration-question/156518/2 "2008-12-29T11:34:57Z")

</div>

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.
