Rails

Check If Particular Attribute Has Changed In A Callback

Posted by Weston Ganger on March 18, 2015

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:


Posted in Rails and Tagged with rails active-record ruby callbacks 


Want me to help develop your next project or application?

Contact Us

Specializing in Ruby-on-Rails, Javascript, Multi-Platform Electron Desktop Apps and Hybrid Cordova Mobile Apps.

Recommended Posts