[英]Rails ActiveRecord conditional callback issue
ruby 2.1.8 rails 3.2.18 红宝石2.1.8导轨3.2.18
I am trying to run a callback when a record is saved only if a particular attribute has been changed. 我正在尝试在仅在特定属性已更改时保存记录时运行回调。 For example
例如
before_save :do_the_thing, if: :my_attr_changed?
However, when I change my_attr
and save, do_the_thing
is not getting called. 但是,当我更改
my_attr
并保存时, do_the_thing
不会被调用。 And yet, if I do the exact same thing, but with: 然而,如果我做同样的事情,但是:
before_save :do_the_thing
def do_the_thing
puts my_attr_changed?
end
It outputs "true" into the logs. 它在日志中输出“true”。 Rather confused here.
在这里相当困惑。 Any help appreciated.
任何帮助赞赏。 Thanks.
谢谢。
把它移到lambda里面吧
before_save :do_the_thing, if: -> { my_attr_changed? }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.