Know what was updated


#1

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


#2

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

http://guides.rails.info/activerecord_validations_callbacks.html#callbacks-overview

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

http://api.rubyonrails.org/classes/ActiveRecord/Dirty.html
http://railscasts.com/episodes/109-tracking-attribute-changes

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”.


#3

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

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

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


#4

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?


#5

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

-philip


#6

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 :slight_smile: 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…