# Association callbacks

**URL:** <https://www.ruby-forum.com/t/association-callbacks/52384>\
**Category:** Rails\
**Created:** [January 13, 2006, 2:36pm UTC](https://www.ruby-forum.com/t/association-callbacks/52384 "2006-01-13T14:36:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [January 13, 2006, 2:36pm UTC](https://www.ruby-forum.com/t/association-callbacks/52384/1 "2006-01-13T14:36:57Z")

</div>

Hello all. I am looking for some feedback on an implementation. I’m  
writing  
a plugin that will log all model changes. I do not want to have to add  
a  
line a code for this functionality every time I create a model. Instead  
I  
create a table with the name \_changes.

The basic stuff was easy:

```
  base.after_create {|model|
    model.log_create if model.has_backup_table?
  }

```

\*\*also log on before\_update and before\_destroy

What I’m curious about is the habtm relationships. I wanted to keep  
track  
of when this changes as well. An example would be changing a user’s  
role.

So I did this:  
def self.included(base)  
…  
base.before\_validation{|model|  
model.class.append\_habtm\_callbacks if model.has\_backup\_table?  
}  
…  
end

module ClassMethods  
def append\_habtm\_callbacks  
reflect\_on\_all\_associations.each{ |association|  
if association.macro == :has\_and\_belongs\_to\_many  
#add\_callback  
add\_callback = “before\_add\_for\_#{association.name}”  
class\_inheritable\_reader(add\_callback.to\_sym)  
write\_inheritable\_array(add\_callback.to\_sym, [:log\_add])  
#remove\_callback  
remove\_callback = “before\_remove\_for\_#{association.name}”  
class\_inheritable\_reader(remove\_callback.to\_sym)  
write\_inheritable\_array(remove\_callback.to\_sym,  
[:log\_remove])  
end  
}  
end  
end#end ClassMethods

This works well and the only way I found to do this without:

1. Didn’t want to have to require code in the model. I wanted it to be  
“smart”.
2. Didn’t want to “alias\_method” any Rails code. I thought using what’s  
available was a better approach in this case.

Thoughts?

Thanks for any feedback,  
andy

p.s. Sorry for the lengthy email…tried to keep it as short as  
possible.

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [February 17, 2006, 2:06am UTC](https://www.ruby-forum.com/t/association-callbacks/52384/2 "2006-02-17T02:06:48Z")

</div>

Hey Andrew!

Rails already has support for association callbacks:

You can read about it in the “association callbacks” section here:

[http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html)

Ciao!  
Florian
