# Auditing mixin for model classes. Small problem

**URL:** <https://www.ruby-forum.com/t/auditing-mixin-for-model-classes-small-problem/71731>\
**Category:** Rails\
**Created:** [February 22, 2006, 7:03am UTC](https://www.ruby-forum.com/t/auditing-mixin-for-model-classes-small-problem/71731 "2006-02-22T07:03:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [February 22, 2006, 7:03am UTC](https://www.ruby-forum.com/t/auditing-mixin-for-model-classes-small-problem/71731/1 "2006-02-22T07:03:44Z")

</div>

I want to include common auditing functionality in my models. This  
involves storing the changes made to a model in a seperate table :

I have created the functions in an Audit module :

module Audit  
before\_update :before\_changes  
after\_update :after\_changes

def before\_changes  
old=self.class.find(id).attributes  
@changes=[]  
attributes.each do |key, val|  
if val != old[key]  
change = Change.new(:primary\_key\_id=\>id,  
:table=\>self.class.table\_name,  
:attribute=\>self.class.human\_attribute\_name(key),  
:old=\>old[key],  
:new=\>val)  
@changes \<\< change  
end  
end  
end

def after\_changes  
if(@changes.size\>0)  
@changes.each {|change| change.save}  
end  
end  
end

I now want to include it like this in my model class :

class Customer \< ActiveRecord::Base  
include Audit  
…  
end

But i get the error : “undefined method ‘before\_update’ for  
Audit::Module”

I can change it like this so it works fine:

class Customer \< ActiveRecord::Base  
include Audit  
before\_update :before\_changes  
after\_update :after\_changes  
…  
end

But this is not an ideal solution. I’d rather only include the one  
include.  
Any ideas???

Thanks,  
Chris

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [February 22, 2006, 7:15am UTC](https://www.ruby-forum.com/t/auditing-mixin-for-model-classes-small-problem/71731/2 "2006-02-22T07:15:04Z")

</div>

I also want to include

“has\_many :changes”

in each model that uses the mixin. I cannot include “has\_many :changes”  
in the module.

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [February 22, 2006, 7:35am UTC](https://www.ruby-forum.com/t/auditing-mixin-for-model-classes-small-problem/71731/3 "2006-02-22T07:35:43Z")

</div>

On 2/22/06, Chris [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I also want to include
> 
> “has\_many :changes”
> 
> in each model that uses the mixin. I cannot include “has\_many :changes”  
> in the module.

I’d highly suggest looking into the various plugins around. But this  
is probably what you’re looking for.

module Audit  
def self.included(base)  
base.before\_save …  
base.has\_many :changes  
end  
end

[http://ruby-doc.org/core/classes/Module.html#M000743](http://ruby-doc.org/core/classes/Module.html#M000743)

–  
Rick O.  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [February 23, 2006, 4:44pm UTC](https://www.ruby-forum.com/t/auditing-mixin-for-model-classes-small-problem/71731/4 "2006-02-23T16:44:40Z")

</div>

The acts\_as\_versioned plugin is pretty close I imagine.

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [February 22, 2006, 7:47am UTC](https://www.ruby-forum.com/t/auditing-mixin-for-model-classes-small-problem/71731/5 "2006-02-22T07:47:17Z")

</div>

Thanks. YOu’re the man.

Have you seen a plugin anywhere that does this?

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [February 23, 2006, 4:50pm UTC](https://www.ruby-forum.com/t/auditing-mixin-for-model-classes-small-problem/71731/6 "2006-02-23T16:50:47Z")

</div>

On 2/23/06, Theodore M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The acts\_as\_versioned plugin is pretty close I imagine.

It looks like you’re using one table to audit all models. Acts as  
Versioned uses a separate table for each model. The nice thing is  
that each versioned record can also act like the actual method…

–  
Rick O.  
[http://techno-weenie.net](http://techno-weenie.net)
