Multiple joins to the same table/association with different conditions

1 view
Skip to first unread message

Michael Schuerig

unread,
Aug 25, 2010, 8:22:26 PM8/25/10
to rubyonra...@googlegroups.com

I'm looking for a nice(!) way to specify multiple joins to the same
table where there are different conditions on each one.

I was hoping for something like this

class Model < ActiveRecord::Base
has_many :things


scope :some_things,
joins(:things).where('things.foo' => 'bar').
joins(:things => :others).where('others.foo' => 'baz')
end

Unfortunately, this is wishful thinking. As far as I can tell, I have to
write the SQL for the second join explicitly like this

joins('JOIN things AS others ON models.id = things.model_id')

Did I miss a better way?

Michael

--
Michael Schuerig
mailto:mic...@schuerig.de
http://www.schuerig.de/michael/

Reply all
Reply to author
Forward
0 new messages