[Rails] Multiple has_many through join table appears multiple times

56 views
Skip to first unread message

all4 miller

unread,
May 6, 2010, 4:23:08 AM5/6/10
to rubyonra...@googlegroups.com
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
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

all4 miller

unread,
May 9, 2010, 1:09:27 PM5/9/10
to rubyonra...@googlegroups.com
ended up using Sales.find(... which generated the correct sql
Reply all
Reply to author
Forward
0 new messages