Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any method available to find changed attributes for objects?

I want to fetch only changed attributes of an object. Is there any method available which returns all the updated attributes?

like image 258
krunal shah Avatar asked Sep 21 '10 15:09

krunal shah


1 Answers

Given there aren't many specifics in your question, I'm going to assume you're referring to ActiveRecord objects. To view the changed attributes on so called "Dirty Objects" you can do the following:

User.changed #=> ["name", "email"]

User.changes #=> { "name" => ["Joe", "Joseph"] }

There are also methods for each attribute if you need to check specific ones.

User.name_changed? #=> true

User.name_change #=> ["Joe", "Joseph"]

More details here: http://ryandaigle.com/articles/2008/3/31/what-s-new-in-edge-rails-dirty-objects

like image 145
Sidane Avatar answered Nov 03 '22 05:11

Sidane