I want to fetch only changed attributes of an object. Is there any method available which returns all the updated attributes?
+4
A:
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
Sidane
2010-09-21 15:39:34
@Sidane (rdb:118) self.misc1_id_changed?true (rdb:118) self.misc1_id_change[5, 5].. Why it's appearing as changed there are no changes in values..
krunal shah
2010-09-21 15:43:37
If the values are the same, the attribute shouldn't be flagged as changed. What modifications are you making to the object prior to checking what has changed?
Sidane
2010-09-21 16:02:37
@Sidane On update i am passing values from my form. I am modifing only one attributes. However, i am getting all the attributes values has changed true.I want to set condition on update. User can only modify some of the attributes. If user will update other attributes than i want to return errors.But getting all the attributes as changed if attributes are not changed. And it's showing the same values.
krunal shah
2010-09-21 16:25:46
Sorry, I'm not sure why that is happening, can you update your question with the relevant code?
Sidane
2010-09-21 21:17:03
@Sidane I am using Flex as my front end. May be that's causing problem in between some where aroung passing objects from rails to flex and flex to rails..
krunal shah
2010-09-21 23:41:02