作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
[#, #]-6ren">
我在模型回调 (after_save) 中,其中一个属性是 BigDecimal 类型。因此,当我更改另一个属性并使用 changes
方法检查脏属性时,我有这个:
{"amount"=>[#<BigDecimal:7f86aa3ac900,'-0.4E3',9(18)>, #<BigDecimal:7f86aa3ac838,'-0.4E3',9(18)>], "description"=>["vvvv", "ccc"]}
它将 amount 实例化为 BigDecimal 并将 object_id 作为更改的一部分。
有没有人知道如何避免这种行为?
最佳答案
如果在 after_save 中你需要检查一个特定的 BigDecimal 字段是否真的改变了,你需要重新加载 rails 创建的方法 attr_name_changed?
(在你的例子中 amount_changed?
) :
def amount_changed?
if amount_change.present?
amount_change[0].to_f != amount_change[1].to_f
end
end
它的作用是比较浮点形式之前 (amount_change[0]
) 和之后 (amount_change[1]
) 的值。
那么在 after_save 回调中你可以这样做:
after_save :do_something_if_amount_changed
def do_something_if_amount_changed
if amount_changed?
do_something
end
end
关于ruby-on-rails - DirtyAttributes 采用更改后的 BigDecimal 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30596778/
我在模型回调 (after_save) 中,其中一个属性是 BigDecimal 类型。因此,当我更改另一个属性并使用 changes 方法检查脏属性时,我有这个: {"amount"=>[#, #]
我是一名优秀的程序员,十分优秀!