views:

263

answers:

2

A while ago I asked (Joins across multiple tables with ActiveRecord with named scopes) http://stackoverflow.com/questions/1189086/joins-across-multiple-tables-with-activerecord-with-named-scopes

Now I need to create a named scope that involved joining across more than two tables, eg:

named_scope :baz_category, lambda {|c| {:joins=>([:foo,:bar,:baz]):conditions=>['baz_cat=',c]}}

Where Foo has one Bar and Bar has one Baz.

I'd like a solution to work for a 4th or 5th etc table.

many many thanks to whoever helps me with this one.

+1  A: 

Have you thought about using eager loading? Documentation is verbose with good examples.

Tate Johnson
thanks...having a look-see
So in Foo, I'll tell that to include Bar and tell Bar to include Baz, making it very easy to jump from Foo to Baz?
+2  A: 

named_scope :baz_category, lambda { |c| :joins => {:foo => { :bar => :baz } }

Nest as deep as you want.

Ryan Bigg
this looks very promising....thanks!!