Ruby-on-rails – How to use Decimal with precision and scale

decimalruby-on-rails

Using rails 3.0.3, I migrated a decimal column in my base using the following migration:

 change_table :products do |t|
   t.change :price, :decimal, :precision => 10, :scale => 2
   # other code
 end

The migration works ok, but I can still store value like 4.64564 where it should only store 4.65

On top of that, except in the migration file I created, schema.rb does not contain info about scale/precision.

Why does rails accept precision/scale migration to ignore it?

Best Answer

You should try with

change_column :products, :price, :decimal, :precision => 10, :scale => 2