views:

385

answers:

4

What's the best practice way for keeping track of who did what in a mid-sized Rails app? Intercepting all database read/writes and storing that in another table?

+1  A: 

If you'd like to "roll your own" solution you can implement database triggers on insert/update/delete that update a separate table. Otherwise there are several commercially supported data audit applications that can be purchased and configured to track and report on these activites for you at the database level.

Malcolm
That works OK for more direct-access DB apps, but it doesn't deal well with the situation described, where many different users at the client end are accessing the database through a server process (or processes) that uses a single database user/password. So the trigger-based solution would record just about everything as having been done by the web server. Which we kinda knew already... ;-)
Mike Woodhouse
No, I think thats a definite candidate when paired up with user_monitor
Omar Qureshi
+7  A: 

you can pretty simply adapt acts_as_versioned to also record information about which user performed the operation. I'd suggest looking into that plugin as versioning is rarely a bad idea.

Ben Hughes
+1, I've used acts_as_versioned for exactly that in several applications.
Sarah Mei
+1 acts_as_versioned_auditable. If you want to learn about it though then I guess you could use callbacks to save the kind of action performed and the user who did it in a seperate table. That's just off the top of my head though and not thought through yet, maybe I will think it through ...
railsninja
+1 @Sarah: Yeah, me too.
Swanand
Cool, I'll look into it. Thanks!
+2  A: 

You can use Observers on callbacks like create/update/delete for several models and save data to another table/model but if you want to have a wiki-like site - acs_as_versioned is better option.

lego
+1  A: 

Rather than reimplementing, you should try some plugins like acts_as_audited

acts_as_audited is an ActiveRecord extension that logs all changes to your models in an audits table.

or PaperTrail.

PaperTrail lets you track changes to your models' data. It's good for auditing or versioning. You can see how a model looked at any stage in its lifecycle, revert it to any version, and even undelete it after it's been destroyed.

Whichever suits your needs.

htanata