# Complex Through Statement

**URL:** <https://www.ruby-forum.com/t/complex-through-statement/56483>\
**Category:** Rails\
**Created:** [March 31, 2006, 5:07pm UTC](https://www.ruby-forum.com/t/complex-through-statement/56483 "2006-03-31T17:07:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gers4302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c2a13f/32.png) [@gers4302](https://www.ruby-forum.com/u/gers4302)\
**Post date:** [March 31, 2006, 5:07pm UTC](https://www.ruby-forum.com/t/complex-through-statement/56483/1 "2006-03-31T17:07:32Z")

</div>

Quick Overview:

I have an ‘Employee’, some ‘Merchants’ and some ‘Products’. A  
‘Merchant’ has many ‘Products’. An ‘Employee’ has multiple ‘Merchants’,  
depending on their relationship. For example, the Employee may be the  
enrollment contact for one merchant and the support contact for another.  
Therefore, my Employee class looks like this:

class Employee \< ActiveRecord::Base

has\_many :enrollment\_merchants, :class\_name =\> “Merchant”,  
:foreign\_key =\> :enrollment\_employee\_id  
has\_many :support\_merchants, :class\_name =\> “Merchant”, :foreign\_key  
=\> :support\_employee\_id

end

And my Merchant class looks like this:

class Merchant \< ActiveRecord::Base

belongs\_to :enrollment\_employee, :class\_name =\> “Employee”,  
:foreign\_key =\> “enrollment\_employee\_id”  
belongs\_to :support\_employee, :class\_name =\> “Employee”, :foreign\_key  
=\> “support\_employee\_id”

has\_many :products

end

I’d like to use :through in the association so that I can get all of the  
enrollment products and all of the support products for the Employee.  
Ideally it would look like:

class Employee \< ActiveRecord::Base

has\_many :enrollment\_merchants, :class\_name =\> “Merchant”,  
:foreign\_key =\> :enrollment\_employee\_id  
has\_many :support\_merchants, :class\_name =\> “Merchant”, :foreign\_key  
=\> :support\_employee\_id

has\_many :enrollment\_products, :through =\> :enrollment\_merchants  
has\_many :support\_products, :through =\> :support\_merchants

end

But that obviously doesn’t work. Can I make this work somehow?

---

<div class="post-metadata">

**Author:** ![gers4302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c2a13f/32.png) [@gers4302](https://www.ruby-forum.com/u/gers4302)\
**Post date:** [March 31, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/complex-through-statement/56483/2 "2006-03-31T19:08:34Z")

</div>

On 3/31/06, Stephen G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

---

<div class="post-metadata">

**Author:** ![gers4302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c2a13f/32.png) [@gers4302](https://www.ruby-forum.com/u/gers4302)\
**Post date:** [March 31, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/complex-through-statement/56483/3 "2006-03-31T19:33:05Z")

</div>

you need to use :source on the :through association

basically the way :through works is that it tries to infer the name of  
the  
name of the association, but in your case it can’t

if you don’t use :source, the has\_many will try to find an association  
called :supported\_products in the mechant class, but it can’t, there is  
only  
:products, and that is what :source does, it says, “look for this  
association in the join class”.

I’m new to :through as well, but this is what it seems to be intended  
for.

class Employee \< ActiveRecord::Base  
has\_many :enrolled\_merchants, :class\_name =\> “Merchant”, :foreign\_key  
=\>  
“enrollment\_employee\_id”  
has\_many :supported\_merchants, :class\_name =\> “Merchant”, :foreign\_key  
=\>  
“support\_employee\_id”  
has\_many :enrolled\_products, :through =\> :enrolled\_merchants, :source  
=\>  
:products  
has\_many :supported\_products, :through =\> :supported\_merchants,  
:source =\>  
:products  
end

class Merchant \< ActiveRecord::Base  
belongs\_to :enrollment\_employee, :class\_name =\> “Employee”  
belongs\_to :support\_employee, :class\_name =\> “Employee”  
has\_many :products  
end

class Product \< ActiveRecord::Base  
belongs\_to :merchant  
end

---

<div class="post-metadata">

**Author:** ![gers4302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c2a13f/32.png) [@gers4302](https://www.ruby-forum.com/u/gers4302)\
**Post date:** [March 31, 2006, 11:38pm UTC](https://www.ruby-forum.com/t/complex-through-statement/56483/4 "2006-03-31T23:38:27Z")

</div>

Thanks, I completely missed that at the bottom of the documentation.  
That worked like a charm.
