Syl*_*Syl 6 ruby-on-rails decimal
使用rails 3.0.3,我decimal使用以下迁移迁移了我的基础中的列:
change_table :products do |t|
t.change :price, :decimal, :precision => 10, :scale => 2
# other code
end
Run Code Online (Sandbox Code Playgroud)
迁移工作正常,但我仍然可以存储像4.64564这样的值,它应该只存储4.65
最重要的是,除了我创建的迁移文件之外,schema.rb不包含有关比例/精度的信息.
为什么rails接受精度/规模迁移忽略它?
我遇到了同样的问题,请查看该库: 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6355 次 |
| 最近记录: |