I had to update some attributes of an object. This can easily be done by my_obj.update_attributes(params). If you want to know the updated attributes …it’s not so easy.

update_attributes internally calls the save-method, so that my_obj.changes can’t be called after update_attributes. My solution was to patch ActiveRecord, so that it additionally returns the changed attributes.

Usage example

status, changes = my_obj.update_attributes_changed(params)

config/initializers/active_record_patch.rb

module ActiveRecord
  class Base
    def update_attributes_changed(attributes)
      self.attributes = attributes
      changes = self.changes
      return save, changes
    end
  end
end
Trackback

only 1 comment untill now

Add your comment now

Switch to our mobile site