# Multiple has\_many through join table appears multiple times

**URL:** <https://www.ruby-forum.com/t/multiple-has-many-through-join-table-appears-multiple-times/188129>\
**Category:** Rails\
**Created:** [May 6, 2010, 10:23am UTC](https://www.ruby-forum.com/t/multiple-has-many-through-join-table-appears-multiple-times/188129 "2010-05-06T10:23:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![all4miller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e95f7d/32.png) [@all4miller](https://www.ruby-forum.com/u/all4miller)\
**Post date:** [May 6, 2010, 10:23am UTC](https://www.ruby-forum.com/t/multiple-has-many-through-join-table-appears-multiple-times/188129/1 "2010-05-06T10:23:08Z")

</div>

Hi All,

I am trying to configure my models to generate sql like this:

select \*  
from location  
inner join sales  
inner join product  
inner join calendar

Currently the models all have this:

class Location…  
has\_many :sales :foreign\_key =\> “location\_id”  
has\_many :products :through =\> :sales  
has\_many :calendars :through =\> :sales

class Product…  
has\_many :sales :foreign\_key =\> “product\_id”  
has\_many :calendars :through =\> :sales  
has\_many :locations :through =\> :sales

class Calendar…  
has\_many :sales :foreign\_key =\> “calendar\_id”  
has\_many :products :through =\> :sales  
has\_many :locations :through =\> :sales

class Sales…  
belongs\_to :calendar  
belongs\_to :product  
belongs\_to :location

In my controller I have this:

Location.find (  
:all,  
:select =\> “calendar.year, location.country, product.name,  
sales.amount”  
:joins =\> [:products, :calendars]  
)

The issue is I get sql like this where the sales table appears twice  
(re-written for brevatiy)

select \*  
from location  
inner join sales  
inner join product  
inner join sales\_calendar  
inner join calendar on sales\_calendar… = calendar…

What must I change to only get the sales table there once i.e.

select \*  
from location  
inner join sales  
inner join product  
inner join calendar

Thanks

---

<div class="post-metadata">

**Author:** ![all4miller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e95f7d/32.png) [@all4miller](https://www.ruby-forum.com/u/all4miller)\
**Post date:** [May 9, 2010, 7:09pm UTC](https://www.ruby-forum.com/t/multiple-has-many-through-join-table-appears-multiple-times/188129/2 "2010-05-09T19:09:27Z")

</div>

ended up using Sales.find(… which generated the correct sql
