# :through relationship

**URL:** <https://www.ruby-forum.com/t/through-relationship/145626>\
**Category:** Ruby\
**Created:** [August 12, 2008, 9:12am UTC](https://www.ruby-forum.com/t/through-relationship/145626 "2008-08-12T09:12:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ganeshk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7cd45c/32.png) [@ganeshk](https://www.ruby-forum.com/u/ganeshk)\
**Post date:** [August 12, 2008, 9:12am UTC](https://www.ruby-forum.com/t/through-relationship/145626/1 "2008-08-12T09:12:02Z")

</div>

Hi,  
Is it possible to use :through in has\_and\_belongs \_to\_many  
relationship

---

<div class="post-metadata">

**Author:** ![ganeshk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7cd45c/32.png) [@ganeshk](https://www.ruby-forum.com/u/ganeshk)\
**Post date:** [August 12, 2008, 1:45pm UTC](https://www.ruby-forum.com/t/through-relationship/145626/2 "2008-08-12T13:45:28Z")

</div>

Ganesh K. wrote:

> Is it possible to use :through in has\_and\_belongs \_to\_many  
> relationship

I guess it’s my turn to apply the rubber stamp!

Ruby is a language. Rails, and ActiveRecord, are frameworks written in  
that  
language. The best forum for questions about them is the  
Ruby-on-Rails-talk  
group at Google G…

And there are those (on that newsgroup) who never use habtm because it  
gets in  
the way the instant you think to add a property to the intermediate  
table. I  
suspect :through only works thru has\_many - not even belongs\_to.

If you only need to access the through item, just write a little  
accessor for it:

def bars  
foos.map(&:bars).flatten  
end

---

<div class="post-metadata">

**Author:** ![ganeshk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7cd45c/32.png) [@ganeshk](https://www.ruby-forum.com/u/ganeshk)\
**Post date:** [August 12, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/through-relationship/145626/3 "2008-08-12T18:33:17Z")

</div>

On Tue, Aug 12, 2008 at 2:09 AM, Ganesh K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi,  
> Is it possible to use :through in has\_and\_belongs \_to\_many  
> relationship

Why would you want to do this?

Todd

---

<div class="post-metadata">

**Author:** ![ganeshk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7cd45c/32.png) [@ganeshk](https://www.ruby-forum.com/u/ganeshk)\
**Post date:** [August 13, 2008, 6:20am UTC](https://www.ruby-forum.com/t/through-relationship/145626/4 "2008-08-13T06:20:23Z")

</div>

On Tuesday 12 August 2008 02:09:36 Ganesh K. wrote:

> Hi,  
> Is it possible to use :through in has\_and\_belongs \_to\_many  
> relationship

This isn’t a Rails forum, but I’ll bite anyway.

I suspect the right way is to simply use “has\_many :through” _instead_  
of the  
habtm relationship. If you’re needing to get through to the other side,  
you  
can (probably) do that, too – at least, I remember this working, the  
last  
time I tried:

has\_many :foos  
has\_many :bars, :through =\> :foos  
has\_many :others, :through =\> :bars

It’s been awhile, though, so I’m not sure.
