19

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

Is there a Rails standard or plugin for this? I feel that it must be there somewhere and I'm just missing it.

6 Answers 6

18

cwninja's answer should do the trick, but there is a little bit more to it.

First of all, the base attribute handling is done with the write_attribute method so you should be tapping into this.

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

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
3
  • Peter, that is beautiful, going to save me a TON of "write_attribute" i have littered all over my code. Thanks!
    – BushyMark
    Oct 4, 2009 at 0:32
  • Seems this is for Rails 2 only.
    – lulalala
    Oct 23, 2013 at 10:41
  • Came to this via google without checking date. This more recent walkthrough did what I needed: How to Track Changes of a Model in a after_callbacks
    – Jay
    Jul 3, 2014 at 19:10
14

For Rails 3:

class MyModel < ActiveRecord::Base
  after_update :my_listener, :if => :my_attribute_changed?

  def my_listener
    puts "Attribute 'my_attribute' has changed"
  end
end

Commented in: https://stackoverflow.com/a/1709956/316700

I don't find official documentation about this.

2
  • 2
    I believe he wants a method to be invoked as soon as an attribute is changed in memory, before save is called, i.e. my_object.some_attr = "foo" would generate a callback.
    – MikeJ
    Jul 5, 2013 at 14:32
  • after_update is deprecated since rails 2.3 (apidock.com/rails/ActiveRecord/Callbacks/after_update)
    – schmijos
    Feb 9, 2015 at 10:20
8

For Rails 4

def attribute=(value)
  super(value)
  your_callback(self.attribute)
end

If you want to overwrite the value of attribute you should use write_attribute(:attribute, your_callback(self.attribute)) and not attribute= or you'll loop over it until you get a stack too deep exception.

5

try:

def attribute_changed(attribute_name, old_value, new_value)
end

def attribute=(attribute_name, value)
  returning(super) do
    attribute_changed(attribute_name, attribute_was(attribute_name), attribute(attribute_name))
  end
end

Just invented this now, but it should work.

0
3

You could always access the private method changed_attributes and check the keys there using a before_save and do with it what you will.

3

The easiest way that I found:

after_save :my_method, :if => Proc.new{ self.my_attribute_changed? }

def my_method
  ... do stuff...
end
2
  • 1
    after_save :my_method, :if => :my_attribute_changed? Dec 18, 2013 at 14:19
  • nice! it's even better :)
    – pesta
    Dec 19, 2013 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.