views:

438

answers:

2

I love making named scopes for rails. however, I ran into somewhat of a pickle. Ive gotten pretty comfortable using named scopes for joins like so:

named_scope :foo, :joins => :bar, :conditions => "bar_attribute = 'something'"

Now pretend I have a table called baz which is contains a foreign key from the bar table. I need something like this:

named_scope :foo, :joins => (:bar => :baz), :conditions => "bar.id = baz.bar_id AND baz_attribute = 'something_else'"

How possible is this?

thanks

A: 

Maybe you can do this in combination with a has_many :through => ... relation.

alltom
how so? could you elaborate a bit?thanks for the help
+2  A: 

You are not that far off. This should work for you:

named_scope: foo, :joins => {:bar => :baz}, :conditions => "bazes.your_attr = 'something_else'"

This assumes that the plural of baz is bazes. You don't need to specify the condition that joins bar.id to bazes.bar_id, it will be inferred from :joins.

BigCanOfTuna
thanks a lot for the help. ill test this and let you know.