# HABTM vs. using has\_many :through

**URL:** <https://www.ruby-forum.com/t/habtm-vs-using-has-many-through/97744>\
**Category:** Rails\
**Created:** [July 10, 2006, 9:25am UTC](https://www.ruby-forum.com/t/habtm-vs-using-has-many-through/97744 "2006-07-10T09:25:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 10, 2006, 9:25am UTC](https://www.ruby-forum.com/t/habtm-vs-using-has-many-through/97744/1 "2006-07-10T09:25:41Z")

</div>

All,

I’ve heard recently about has\_many :through as a necessary alternative  
to HABTM (when the join table has it’s own id column, say, in a legacy  
schema). However, is the prevailing Rails wisdom now that one should  
use has\_many :through in _all_ cases?

If so, why?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 10, 2006, 10:03am UTC](https://www.ruby-forum.com/t/habtm-vs-using-has-many-through/97744/2 "2006-07-10T10:03:46Z")

</div>

Uber associationatire Josh S. has written a much recommended article  
outlining the features and pros/cons of habtm and hmt:  
[http://blog.hasmanythrough.com/articles/2006/04/20/many-to-many-dance-off](http://blog.hasmanythrough.com/articles/2006/04/20/many-to-many-dance-off).

> However, is the prevailing Rails wisdom now that one should  
> use has\_many :through in _all_ cases?  
> As with a lot of Rails wisdom the short answer is ‘it depends’ :0)

Steve

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 11, 2006, 2:33am UTC](https://www.ruby-forum.com/t/habtm-vs-using-has-many-through/97744/3 "2006-07-11T02:33:05Z")

</div>

It may be heresy to some, but I still prefer HABTM over  
has\_many :through if the relationship I’m dealing with is simple.  
i.e., if there’s no reason to store any metainfo in a join model, I  
just use HABTM. I like my proxy collections!

This doesn’t mean I don’t love (and use) has\_many :through for  
situations when there does need to be metainfo stored about the  
relationship. But I always ask myself first before I decide which to  
use.

-Seth

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 11, 2006, 2:42am UTC](https://www.ruby-forum.com/t/habtm-vs-using-has-many-through/97744/4 "2006-07-11T02:42:49Z")

</div>

Agreed.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 11, 2006, 10:05am UTC](https://www.ruby-forum.com/t/habtm-vs-using-has-many-through/97744/5 "2006-07-11T10:05:43Z")

</div>

Wes G. wrote:

> All,
> 
> I’ve heard recently about has\_many :through as a necessary alternative  
> to HABTM (when the join table has it’s own id column, say, in a legacy  
> schema). However, is the prevailing Rails wisdom now that one should  
> use has\_many :through in _all_ cases?

Not at all.

Think about the model first. Do you want to model your associations  
explicitly, using a model class? In other words, are there things that  
you want to say about the associations? (Can you put a name to them?  
This may help you to find things that you might want to say about them.)  
If so, use a model for the association… and then you will need the  
:through capability to let you navigate through the association in a  
single query.

If you just want the association to exist, with nothing further being  
said about it, stick with HABTM.

In other words, I agree with others in this thread, but would like to  
put the emphasis on thinking carefully about the domain model.

regards

Justin
