Rails 3 check if attribute changed

pcasa picture pcasa · Feb 19, 2011 · Viewed 113.1k times · Source

Need to check if a block of attributes has changed before update in Rails 3.

street1, street2, city, state, zipcode

I know I could use something like

if @user.street1 != params[:user][:street1]
  then do something....
end

But that piece of code will be REALLY long. Is there a cleaner way?

Answer

Peter Brown picture Peter Brown · Feb 19, 2011

Check out ActiveModel::Dirty (available on all models by default). The documentation is really good, but it lets you do things such as:

@user.street1_changed? # => true/false