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?
You should try with
change_column :products, :price, :decimal, :precision => 10, :scale => 2
I had the same problem, please look at that lib: https://github.com/dmgr/dmg_decimal
With it you can use it in a model like that:
def price= val
self[:price] = Dmg::Decimal.real(val, scale: 2, precision: 10).to_d if val.present?
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With