# Has\_many :through

**URL:** <https://www.ruby-forum.com/t/has-many-through/172855>\
**Category:** Rails\
**Created:** [July 29, 2009, 12:25am UTC](https://www.ruby-forum.com/t/has-many-through/172855 "2009-07-29T00:25:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![uddun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/f19dbf/32.png) [@uddun](https://www.ruby-forum.com/u/uddun)\
**Post date:** [July 29, 2009, 12:25am UTC](https://www.ruby-forum.com/t/has-many-through/172855/1 "2009-07-29T00:25:02Z")

</div>

I want to have the following relationships set up and, for the life of  
me, I can’t get the bloody things to work:

class Company \< ActiveRecord::Base

has\_many :clients, :class\_name =\> ‘Company’  
has\_many :suppliers, :class\_name =\> ‘Company’

end

I have two tables set up called company\_clients and company\_suppliers  
that each contain company\_id and either client\_id or supplier\_id.

Any ideas on how I can get this to work? I’ve come up with workarounds  
for it but none of them are elegant and I’d much rather do it properly.

Any help would be much appreciated.

---

<div class="post-metadata">

**Author:** ![uddun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/f19dbf/32.png) [@uddun](https://www.ruby-forum.com/u/uddun)\
**Post date:** [July 29, 2009, 1:28am UTC](https://www.ruby-forum.com/t/has-many-through/172855/2 "2009-07-29T01:28:04Z")

</div>

On Tue, Jul 28, 2009 at 6:25 PM, Paul  
Graham[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Company \< ActiveRecord::Base
> 
> has\_many :clients, :class\_name =\> ‘Company’  
> has\_many :suppliers, :class\_name =\> ‘Company’
> 
> end
> 
> I have two tables set up called company\_clients and company\_suppliers  
> that each contain company\_id and either client\_id or supplier\_id.

has\_many (along with a belongs\_to in the other class) if for a 1-n  
relation, but you are modelling an n-m relation.

class Company \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :clients, :class\_name =\> “Company”,  
:join\_table =\> :company\_clients  
has\_and\_belongs\_to\_many :suppliers, :class\_name =\> “Company”,  
:join\_table =\> :company\_suppliers  
end

I’m not entirely sure that this works, but it’s more likely to.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![uddun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/f19dbf/32.png) [@uddun](https://www.ruby-forum.com/u/uddun)\
**Post date:** [July 29, 2009, 10:20pm UTC](https://www.ruby-forum.com/t/has-many-through/172855/3 "2009-07-29T22:20:04Z")

</div>

Rick Denatale wrote:

> has\_many (along with a belongs\_to in the other class) if for a 1-n  
> relation, but you are modelling an n-m relation.
> 
> class Company \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many :clients, :class\_name =\> “Company”,  
> :join\_table =\> :company\_clients  
> has\_and\_belongs\_to\_many :suppliers, :class\_name =\> “Company”,  
> :join\_table =\> :company\_suppliers  
> end
> 
> I’m not entirely sure that this works, but it’s more likely to.
> 
> –  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

Rick, thank you. With one small amendment, that does the job nicely. The  
only thing that was missing was “:association\_foreign\_key =\> :client\_id”  
tacked onto the declaration (and a corresponding one for the suppliers).
