views:

22

answers:

1

Hi all,

Is there a way I can know what attributes are updated in my model? I only want to do a validation on a user when a certain attribute was posted. This would actually be the hash that is sent via the params eg:

@user.update_attributes(params[:user])

Thanks.

+2  A: 

This is available within the ActiveRecord::Dirty module, which is included by default.

The changed method will give you a list of attributes with unsaved changes. The changes method will give you a hash of unsaved changes, where the keys are the attribute names and the values are an array consisting of the original and new value.

For example:

@user.changed # => ['name', 'age']  
@user.changes # => { 'name' => ['Bill', 'John'], 'age' => [18, 21] }
John Topley