# A has\_many :through (B, C)

**URL:** <https://www.ruby-forum.com/t/a-has-many-through-b-c/133360>\
**Category:** Rails\
**Created:** [March 30, 2008, 10:44pm UTC](https://www.ruby-forum.com/t/a-has-many-through-b-c/133360 "2008-03-30T22:44:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 30, 2008, 10:44pm UTC](https://www.ruby-forum.com/t/a-has-many-through-b-c/133360/1 "2008-03-30T22:44:00Z")

</div>

Hi,

I’m wondring if it is possible to have a “has\_many” through two things  
at the same time.

For instance:

If I do this,

* * *

class A  
has\_many :blops # blobs have things  
has\_many :blups # blups has things too

## has\_many :things, :through =\> :blops has\_many :things, :through =\> :blups end

only the “things” through “blups” get retrieved (only the last hmt is  
considered by AR).

I tried “has\_many :things, :through =\> [:blops, :blups]” too…

How can I do this and get all the things through blops and blups?

Thanks!

aurels

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 31, 2008, 12:44am UTC](https://www.ruby-forum.com/t/a-has-many-through-b-c/133360/2 "2008-03-31T00:44:19Z")

</div>

Aurels wrote:

> class A  
> has\_many :blops # blobs have things  
> has\_many :blups # blups has things too
> 
> has\_many :things, :through =\> :blops  
> has\_many :things, :through =\> :blups  
> end

The blop things and the blup things are really different associations,  
so you can do:

class A  
has\_many :blops  
has\_many :blups

has\_many :blop\_things, :through =\> :blops, :source =\> :thing  
has\_many :blup\_things, :through =\> :blups, :source =\> :thing  
end

If you need to access all of the things together, then you can add an  
accessor:

def things  
blop\_things + blup\_things  
end

However you won’t be able to use the association methods on this (ie.,  
#find, #sum, etc…)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 2, 2008, 7:31am UTC](https://www.ruby-forum.com/t/a-has-many-through-b-c/133360/3 "2008-04-02T07:31:17Z")

</div>

You can use find… but it’ll be the ruby array find… not the  
association AR find… and you can use SUM, as it is inside Enumerable  
class, not the association proxy.

Julian

Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW VIDEO  
OUT 3rd APRIL  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 1, 2008, 12:15pm UTC](https://www.ruby-forum.com/t/a-has-many-through-b-c/133360/4 "2008-04-01T12:15:33Z")

</div>

Yes it works. But It would be great to be able to use #find…  
Thanks!

On Mar 31, 12:44 am, Mark B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
