# How to Observe a Habtm relationship?

**URL:** <https://www.ruby-forum.com/t/how-to-observe-a-habtm-relationship/144328>\
**Category:** Rails\
**Created:** [July 28, 2008, 6:30pm UTC](https://www.ruby-forum.com/t/how-to-observe-a-habtm-relationship/144328 "2008-07-28T18:30:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [July 28, 2008, 6:30pm UTC](https://www.ruby-forum.com/t/how-to-observe-a-habtm-relationship/144328/1 "2008-07-28T18:30:58Z")

</div>

Railsters:

ActiveRecord::Observer is a cute little class that lets you bundle  
groups of  
after\_create and before\_update calls together. Don’t call us we’ll call  
you.

How can it observe a has\_and\_belongs\_to\_many relationship? Mechanically,  
we need  
to observe changes in the secret models\_models table between the two  
Models.

–  
Phlip

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [July 28, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/how-to-observe-a-habtm-relationship/144328/2 "2008-07-28T18:54:28Z")

</div>

that’s one of the reasons, why many people (like me)  
nearly never use habtm directly, but make the effort  
to create the intermediate model by hand and use  
the :through option for the final definition.

Maybe there is another trick to get this work, but  
the model variant is the common problem solver  
in this case. the extra work isn’t that much and you  
get a lot of functionality for it. For example you can  
add status flags or other additional data to the model  
and have more freedom handling creating & deleting  
the relationship records.

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [July 28, 2008, 6:56pm UTC](https://www.ruby-forum.com/t/how-to-observe-a-habtm-relationship/144328/3 "2008-07-28T18:56:48Z")

</div>

+1… the hardest part is coming up with the name of the model, but from  
there it’s much nicer. 🙂

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [July 28, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/how-to-observe-a-habtm-relationship/144328/4 "2008-07-28T20:10:02Z")

</div>

Brian H. wrote:

> +1… the hardest part is coming up with the name of the model, but from  
> there it’s much nicer. 🙂

Been there done that. But are we admitting a gap in ActiveRecord’s  
contract?

Confer the habtm directive already supports :after\_add =\> :my\_method. We  
discovered this gap by attempting to move :my\_method into the common  
observer…

–  
Phlip
