# Know what was updated

**URL:** <https://www.ruby-forum.com/t/know-what-was-updated/169013>\
**Category:** Rails\
**Created:** [May 28, 2009, 12:42pm UTC](https://www.ruby-forum.com/t/know-what-was-updated/169013 "2009-05-28T12:42:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![blueomega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@blueomega](https://www.ruby-forum.com/u/blueomega)\
**Post date:** [May 28, 2009, 12:42pm UTC](https://www.ruby-forum.com/t/know-what-was-updated/169013/1 "2009-05-28T12:42:42Z")

</div>

I am doing a rss feed who reports all the changes in the program.

all changes are sent to a database where the afected item, who made  
changed, time of change and a string reporting the change is kept

the problem is that the string who reports the change is too generic,  
something like: Task ‘xtask’ was updated

i would like to know if its possible to know what was changed with  
update\_attributes (i know it updates all the attributes), without  
needing to check the before and after in the database

the hash parameters for update\_attributes come from a form\_for

---

<div class="post-metadata">

**Author:** ![blueomega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@blueomega](https://www.ruby-forum.com/u/blueomega)\
**Post date:** [May 28, 2009, 5:15pm UTC](https://www.ruby-forum.com/t/know-what-was-updated/169013/2 "2009-05-28T17:15:06Z")

</div>

First, get familiar with model callbacks if you’re not already because  
you don’t want to put this logic in the controller:

> **[rails.info](http://guides.rails.info/activerecord_validations_callbacks.html#callbacks-overview)**
>
> Ruby on Rails,Rails Tutorial,Ruby Basics,Rails 4,Bar Rails,Rail

Then check out the dirty object feature that ActiveRecord provides and  
how to use it:

[http://api.rubyonrails.org/classes/ActiveRecord/Dirty.html](http://api.rubyonrails.org/classes/ActiveRecord/Dirty.html)

> **[\#109 Tracking Attribute Changes - RailsCasts](http://railscasts.com/episodes/109-tracking-attribute-changes)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

With that said, I’d probably write an “after\_update” callback on  
whatever model you’re using and interrogate that object with what  
“model.changed”.

---

<div class="post-metadata">

**Author:** ![blueomega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@blueomega](https://www.ruby-forum.com/u/blueomega)\
**Post date:** [May 28, 2009, 5:41pm UTC](https://www.ruby-forum.com/t/know-what-was-updated/169013/3 "2009-05-28T17:41:28Z")

</div>

y, i´ve already updated the item model with Callbacks, the controller  
code was becoming ugly

[http://api.rubyonrails.org/classes/ActiveRecord/Dirty.html](http://api.rubyonrails.org/classes/ActiveRecord/Dirty.html) seems to do  
the job

[#109 Tracking Attribute Changes - RailsCasts](http://railscasts.com/episodes/109-tracking-attribute-changes) seems  
also a good explanation, but i cant listen here, in a library, with no  
phones

i think its solved

thx

---

<div class="post-metadata">

**Author:** ![blueomega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@blueomega](https://www.ruby-forum.com/u/blueomega)\
**Post date:** [May 28, 2009, 8:02pm UTC](https://www.ruby-forum.com/t/know-what-was-updated/169013/4 "2009-05-28T20:02:26Z")

</div>

That’s a great point Philip. I’m actually trying to solve that very  
problem in a project I’m working on.

I want to be able to do exactly what lighthouseapp does when it logs  
ticket updates. In that case, you have comments that people can make  
on a ticket. But when someone updates the ticket, I want to create a  
comment and log who changed what. I’ve now learned you can’t do that  
with a simple model observer (because I need to know who is updating  
the ticket, which is part of the session).

I searched around on this list and the only thing I’ve found with any  
substance is people recommending the cache sweeping recipe from the  
book rails recipes. I’m digging into that now…

In your case though, you’re creating a separate model (AuditLog) in  
the controller…I want to keep this out of the controller.

Do you have any experience with cache sweeping for this purpose?

---

<div class="post-metadata">

**Author:** ![blueomega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@blueomega](https://www.ruby-forum.com/u/blueomega)\
**Post date:** [May 28, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/know-what-was-updated/169013/5 "2009-05-28T18:45:30Z")

</div>

On May 28, 2009, at 8:13 AM, JL Smith wrote:

> First, get familiar with model callbacks if you’re not already because  
> you don’t want to put this logic in the controller:

You might… the problem with the callbacks is you don’t get access to  
the user who did it (depending on how they’ve logged in), nor can you  
ignore any trivial changes – although that might not be a requirement  
here…

Below was my solution for selectively logging actions…

[http://pjkh.com/articles/2009/02/02/creating-an-audit-log-in-rails](http://pjkh.com/articles/2009/02/02/creating-an-audit-log-in-rails)

-philip

---

<div class="post-metadata">

**Author:** ![blueomega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@blueomega](https://www.ruby-forum.com/u/blueomega)\
**Post date:** [May 28, 2009, 8:45pm UTC](https://www.ruby-forum.com/t/know-what-was-updated/169013/6 "2009-05-28T20:45:20Z")

</div>

> I searched around on this list and the only thing I’ve found with any  
> substance is people recommending the cache sweeping recipe from the  
> book rails recipes. I’m digging into that now…
> 
> In your case though, you’re creating a separate model (AuditLog) in  
> the controller…I want to keep this out of the controller.
> 
> Do you have any experience with cache sweeping for this purpose?

Honestly it was long enough back I don’t remember 🙂 It might have  
been that I didn’t want to log all updates, only at certain times that  
had me keep it out of any cache/observers…
