Check If Particular Attribute Has Changed In A Callback

Posted by Weston Ganger

Sometimes its handy to do a particular task if an attribute has changed in a model.

You probably only want it to run when an instance is updated, so choose your callback accordingly.


class Post < ActiveRecord::Base
  after_update :notify, :if => :my_attribute_changed?

  def notify
    puts "The Attribute 'my_attribute' has been changed!"
  end
end


Helpful Links:

Article Topic:Software Development - Rails

Date:March 18, 2015

Recommended Posts

Contact

604-309-8713
info@solidfoundationwebdev.com

Send Me A Message

I would love to get in touch and talk about your next project. Feel free to send a message and I will get back to you shortly.

Get Connected