# Audit tables?

**URL:** <https://www.ruby-forum.com/t/audit-tables/113425>\
**Category:** Rails\
**Created:** [August 30, 2007, 11:48pm UTC](https://www.ruby-forum.com/t/audit-tables/113425 "2007-08-30T23:48:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcbey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@marcbey](https://www.ruby-forum.com/u/marcbey)\
**Post date:** [August 30, 2007, 11:48pm UTC](https://www.ruby-forum.com/t/audit-tables/113425/1 "2007-08-30T23:48:59Z")

</div>

Hi,

I would like to create audit tables in my RoR/mysql app. I would like  
to  
start out auditing just about everything: every change in the database  
should result in a row in (one or more) audit tables. My app requires  
login, so I would like to include the user\_id in all entries.

Is there a good starting point for this or should I simply design the  
table(s), build triggers, etc.?

Thanks,

Marc

---

<div class="post-metadata">

**Author:** ![marcbey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@marcbey](https://www.ruby-forum.com/u/marcbey)\
**Post date:** [August 31, 2007, 1:19am UTC](https://www.ruby-forum.com/t/audit-tables/113425/2 "2007-08-31T01:19:07Z")

</div>

On 8/30/07, Marc B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I would like to create audit tables in my RoR/mysql app. I would like to  
> start out auditing just about everything: every change in the database  
> should result in a row in (one or more) audit tables. My app requires  
> login, so I would like to include the user\_id in all entries.
> 
> Is there a good starting point for this or should I simply design the  
> table(s), build triggers, etc.?

You need to learn about AR’s callbacks  
([ActiveRecord::Callbacks](http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html)),  
which are “hooks” where you can add the auditing code.

Also, google for “rails created\_by” for some articles related to the  
issue of getting the user id. It’s not as easy as it looks, because  
the model code (where the callbacks are) doesn’t really have access to  
the controller (where the user id is). It can be done (I’m doing it),  
but you need to read up on the various considerations, especially  
thread-safety.

HTH

---

<div class="post-metadata">

**Author:** ![marcbey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@marcbey](https://www.ruby-forum.com/u/marcbey)\
**Post date:** [August 31, 2007, 2:13am UTC](https://www.ruby-forum.com/t/audit-tables/113425/3 "2007-08-31T02:13:11Z")

</div>

There is this plugin I found from a google search. I’ve not used it,  
but check it out and see if it’s what you need.

[http://opensoul.org/2006/7/21/acts\_as\_audited](http://opensoul.org/2006/7/21/acts_as_audited)

---

<div class="post-metadata">

**Author:** ![marcbey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@marcbey](https://www.ruby-forum.com/u/marcbey)\
**Post date:** [August 31, 2007, 6:32am UTC](https://www.ruby-forum.com/t/audit-tables/113425/4 "2007-08-31T06:32:28Z")

</div>

I have a similar requirement as I’m working on a database system for  
clinical trials. I’ve investigated the plugins mentioned but I wasn’t  
too comfortable for many reasons, especially with the ability to  
reconstruct from audit trail.

I’m actually looking into extending the ActiveRecord.

---

<div class="post-metadata">

**Author:** ![marcbey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@marcbey](https://www.ruby-forum.com/u/marcbey)\
**Post date:** [August 31, 2007, 1:19pm UTC](https://www.ruby-forum.com/t/audit-tables/113425/5 "2007-08-31T13:19:32Z")

</div>

Joon You wrote:

> I have a similar requirement as I’m working on a database system for  
> clinical trials. I’ve investigated the plugins mentioned but I wasn’t  
> too comfortable for many reasons, especially with the ability to  
> reconstruct from audit trail.
> 
> I’m actually looking into extending the ActiveRecord.

Observers are the way to go if you require this kind of functionality

---

<div class="post-metadata">

**Author:** ![marcbey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@marcbey](https://www.ruby-forum.com/u/marcbey)\
**Post date:** [August 31, 2007, 1:25am UTC](https://www.ruby-forum.com/t/audit-tables/113425/6 "2007-08-31T01:25:08Z")

</div>

> I would like to create audit tables in my RoR/mysql app. I would like to  
> start out auditing just about everything: every change in the database  
> should result in a row in (one or more) audit tables. My app requires  
> login, so I would like to include the user\_id in all entries.
> 
> Is there a good starting point for this or should I simply design the  
> table(s), build triggers, etc.?

[http://agilewebdevelopment.com/plugins/search?search=audit](http://agilewebdevelopment.com/plugins/search?search=audit)
