# Habtm vs. has\_many/belongs\_to

**URL:** <https://www.ruby-forum.com/t/habtm-vs-has-many-belongs-to/55672>\
**Category:** Rails\
**Created:** [March 22, 2006, 9:52pm UTC](https://www.ruby-forum.com/t/habtm-vs-has-many-belongs-to/55672 "2006-03-22T21:52:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [March 22, 2006, 9:52pm UTC](https://www.ruby-forum.com/t/habtm-vs-has-many-belongs-to/55672/1 "2006-03-22T21:52:44Z")

</div>

In AWDWR David says (pg 232),

"When a Join Wants to Be a Model:  
While a many-to-many relation with attributes can often seem like the  
obvious choice, itâ??s often a mirage for a missing domain model. When  
it is, it can be advantageous to convert this relationship into a  
real model  
and decorate it with a richer set of behavior. This lets you  
accompany the  
data with methods.  
As an example, we could turn the articles\_users relationship into a new  
model called Reading. This Reading model will belong\_to both an article  
and a user. And itâ??s now the obvious spot to place methods such as  
find\_popular\_articles( ), which can perform a group by quer y and  
return the  
articles that have been read the most. This lightens the burden on  
the Arti-  
cle model and turns the concept of popularity into a separated concern  
that naturally sits with the Reading model. "

First, I assume this means that the other model definitions need to  
have a “has\_many:” declaration for this new model that will belong to  
each?

Is anyone aware of where I might look to find more information on  
this kind of alternative setup? How often have any of you chosen the  
method described here as opposed to just using HABTM? What are some  
of the main trade-offs in using either?

Thanks in advance!

Michael J.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [March 22, 2006, 10:44pm UTC](https://www.ruby-forum.com/t/habtm-vs-has-many-belongs-to/55672/2 "2006-03-22T22:44:57Z")

</div>

Michael,

Generally the switch from HABTM to a Model Join is when the link will  
actually store extra data besides the link information. The example  
above references adding popularity to the link, which prompted the  
writer to switch. There is a way to push that extra data into the join  
while using HABTM but it can get ugly fast and usually the user will  
want to add data from an interface which lends itself to having a  
Model Join.

The practice that the example is promoting is to create the Model Join  
whenever you have extra data in the joining table.

Carl

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [March 22, 2006, 10:59pm UTC](https://www.ruby-forum.com/t/habtm-vs-has-many-belongs-to/55672/3 "2006-03-22T22:59:55Z")

</div>

Michael J. wrote:

> First, I assume this means that the other model definitions need to  
> have a “has\_many:” declaration for this new model that will belong to  
> each?
> 
> Is anyone aware of where I might look to find more information on  
> this kind of alternative setup? How often have any of you chosen the  
> method described here as opposed to just using HABTM? What are some  
> of the main trade-offs in using either?

I blogged about has\_many :through associations to kick off my blog last  
month. I think my article answers many of your questions.

[http://blog.hasmanythrough.com/articles/2006/02/28/association-goodness](http://blog.hasmanythrough.com/articles/2006/02/28/association-goodness)

Since then 1.1 got a new kind of has\_many :through where the middle  
table looks like

class Middle \< AR  
belongs\_to :left  
has\_many :rights  
end

instead of

class Middle \< AR  
belongs\_to :left  
belongs\_to :right  
end

I’ll try and write something about that soon, cause it’s nifty.

–josh  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
