all4 miller
2010-May-06 08:23 UTC
Multiple has_many through join table appears multiple times
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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
all4 miller
2010-May-09 17:09 UTC
Re: Multiple has_many through join table appears multiple times
ended up using Sales.find(... which generated the correct sql -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.