Hi,
changed_attributes method provides a useful functionality to check the value that was updated in a column in before the object being saved.
These methods available under the "Active Model Dirty"
Here is the clear explanation on it.
person = Person.find_by_name('Uma Mahesh')
person.changed? # => false
person.name = 'Varma'
person.changed? # => true
person.name_changed? # => true
person.name_was # => 'Uma Mahesh'
person.name_change # => ['Uma Mahesh', 'Varma']
A clear explanation of these methods were explained in the url :
http://api.rubyonrails.org/classes/ActiveModel/Dirty.html#method-i-changed_attributes
This method can be used to check the column value was updated or not. If you want to check the specific column value was update or not, this can be done by as below.
In your model:
before_update :update_image_count_counter
def update_image_count_counter
if changed_attributes.keys.include?("image_count")
# your required code comes here
end
end
Thank You,
Uma Mahesh
No comments:
Post a Comment