使用rails 3.0.3,我使用以下迁移迁移了我的基础中的decimal
列:
change_table :products do |t|
t.change :price, :decimal, :precision => 10, :scale => 2
# other code
end
迁移工作正常,但我仍然可以存储像4.64564这样的值,它应该只存储4.65
除此之外,除了我创建的迁移文件外,schema.rb不包含有关比例/精度的信息。
为什么rails接受精度/规模迁移以忽略它?
答案 0 :(得分:2)
你应该试试
change_column :products, :price, :decimal, :precision => 10, :scale => 2
答案 1 :(得分:1)
我有同样的问题,请查看那个lib: https://github.com/dmgr/dmg_decimal
有了它,你可以在这样的模型中使用它:
def price= val
self[:price] = Dmg::Decimal.real(val, scale: 2, precision: 10).to_d if val.present?
end