Checking if an attribute has changed after commit in Rails

I know using is dirty

either changed?

useful in a situation before_commit

or prior to saving the model. After saving the model previous_changes

gives changes. It's great. But,

How to check if a specific attribute has been changed in a controller if it looks like

def update
 @item = Item.find(params[:id])
if @item.update_attributes(item_params)
 # horray
 # check if the :name changed, if so do something about it
 # d'oh


Is there a better way than doing !@item.previous_changes[:name].nil?


source to share

1 answer

I don't know if there is a better way, but I would do:

if @item.previous_changes.has_key?('name')
  # Name has been changed.




All Articles