问题描述
我知道 ActiveRecord::Dirty 和相关方法,但我没有看到可以订阅属性更改事件的方法.比如:
I am aware of ActiveRecord::Dirty and the related methods, but I don't see a means by which I can subscribe to an attribute changed event. Something like:
class Person < ActiveRecord::Base
def attribute_changed(attribute_name, old_value, new_value)
end
#or
attribute_changed do |attribute_name, old_value, new_value|
end
end
有这方面的 Rails 标准或插件吗?我觉得它一定在某个地方,我只是想念它.
Is there a Rails standard or plugin for this? I feel that it must be there somewhere and I'm just missing it.
推荐答案
cwninja 的答案应该可以解决问题,但还有更多.
cwninja's answer should do the trick, but there is a little bit more to it.
首先,基本属性处理是使用 write_attribute 方法完成的,因此您应该利用它.
First of all, the base attribute handling is done with the write_attribute method so you should be tapping into this.
Rails 也确实有一个内置的回调结构,虽然它不允许传递参数,这有点令人烦恼.
Rails also does have a built in callback structure which could be nice to tap into though it doesn't allow for passing arguments which is a bit of an annoyance.
使用自定义回调你可以这样做:
Using custom callbacks you could do it like this:
class Person < ActiveRecord::Base
def write_attribute(attr_name, value)
attribute_changed(attr_name, read_attribute(attr_name), value)
super
end
private
def attribute_changed(attr, old_val, new_val)
logger.info "Attribute Changed: #{attr} from #{old_val} to #{new_val}"
end
end
如果您想尝试使用 Rails 回调(如果您可能有多个回调和/或子类化特别有用),您可以执行以下操作:
If you wanted to try to use Rails callbacks (especially useful if you might have multiple callbacks and/or subclassing) you could do something like this:
class Person < ActiveRecord::Base
define_callbacks :attribute_changed
attribute_changed :notify_of_attribute_change
def write_attribute(attr_name, value)
returning(super) do
@last_changed_attr = attr_name
run_callbacks(:attribute_changed)
end
end
private
def notify_of_attribute_change
attr = @last_changed_attr
old_val, new_val = send("#{attr}_change")
logger.info "Attribute Changed: #{attr} from #{old_val} to #{new_val}"
end
end
这篇关于回调更改的 ActiveRecord 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!