# Detect changes with update\_attributes

**URL:** <https://www.ruby-forum.com/t/detect-changes-with-update-attributes/184343>\
**Category:** Rails\
**Created:** [March 1, 2010, 9:27am UTC](https://www.ruby-forum.com/t/detect-changes-with-update-attributes/184343 "2010-03-01T09:27:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeonS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@LeonS](https://www.ruby-forum.com/u/LeonS)\
**Post date:** [March 1, 2010, 9:27am UTC](https://www.ruby-forum.com/t/detect-changes-with-update-attributes/184343/1 "2010-03-01T09:27:39Z")

</div>

Hi,

how can i detect changed attributes, when i’m using update\_attributes  
in the update method of my controller?  
A solution in the model would be very nice. Tanks

greetings  
Leon

---

<div class="post-metadata">

**Author:** ![LeonS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@LeonS](https://www.ruby-forum.com/u/LeonS)\
**Post date:** [March 1, 2010, 9:51am UTC](https://www.ruby-forum.com/t/detect-changes-with-update-attributes/184343/2 "2010-03-01T09:51:39Z")

</div>

You can do it with update\_attributes as that updates and saves in one  
go.

You can do this instead

@thing.attributes=(params[:thing])  
if @thing.changed?  
@thing.save  
else  
…something else  
end

All the best

RobL

On 1 March 2010 08:26, LeonS [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You received this message because you are subscribed to the Google G. “Ruby on Rails: Talk” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to removed\_email\_address@domain.invalid.  
> For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
Rob L.  
removed\_email\_address@domain.invalid

> **[Posts](https://www.robl.me/)**
>
> I write thems codes

---

<div class="post-metadata">

**Author:** ![LeonS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@LeonS](https://www.ruby-forum.com/u/LeonS)\
**Post date:** [March 1, 2010, 10:07am UTC](https://www.ruby-forum.com/t/detect-changes-with-update-attributes/184343/3 "2010-03-01T10:07:27Z")

</div>

Hi,

I thought about your solution and now I think i’ve got a better  
solution, what do you think about that?  
I just overwrite the update\_attributes method and after i assigned the  
attributes, i can check for changes.

def update\_attributes(attributes)  
self.attributes = attributes  
if self.changed?  
#kickoff something…  
end  
save  
end

What do you think about this solution?

---

<div class="post-metadata">

**Author:** ![LeonS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@LeonS](https://www.ruby-forum.com/u/LeonS)\
**Post date:** [March 1, 2010, 10:34am UTC](https://www.ruby-forum.com/t/detect-changes-with-update-attributes/184343/4 "2010-03-01T10:34:58Z")

</div>

On 1 March 2010 09:06, LeonS [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def update\_attributes(attributes)  
> self.attributes = attributes  
> if self.changed?  
> #kickoff something…  
> end  
> save  
> end
> 
> What do you think about this solution?

The problem with both yours and Rob’s solution is that you’re doing  
your post-update checks in one place, when there’s more than one place  
those updates can happen. Your modification of Rob’s suggestion is  
better, because at least you’re checking in the model, rather than in  
the controller.

But you might be better off doing your check regardless of where the  
updates have come from (either a single attribute being assigned, or a  
mass allocation), so put a “before\_save” filter in your model:

before\_save :check\_changed  
def check\_changed  
puts (changed? ? “changed” : “unchanged”)  
end

Of course, this might _cause_ problems for your specific requirement,  
if, for instance, there’s certain fields you want to ignore changes  
for. But you can always expand the “check\_changed” method to do more  
work; check the changed\_attributes hash, etc.
