回调改变了ActiveRecord的属性? [英] Callback for changed ActiveRecord attributes?

查看:115
本文介绍了回调改变了ActiveRecord的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道的ActiveRecord ::肮脏和相关的方法,但我没有看到一个由我可以订阅一个改变了属性事件的手段。是这样的:

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的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆