Order by distantly related table

I have 2 tables (toms and dicks) joined with has_many through a third table (harries). Table tunnels is related to table dicks (table tunnels has many dicks).

I would like to select a record from toms with all related records from dicks ordered a field in tunnels.

Something like:

@tom = Tom.find(1, :include => :dick.tunnels, :order_by => tunnel.length)

Any ideas?

Mike

I'm not 100% ssure about the syntax for eager loading of distant tables, but it should look something like that:

@tom = Tom.find(1, :include => {:dicks => [:tunnels]}, :order_by => "tunnels.length ASC")

could be, the {} should be

I'm not 100% ssure about the syntax for eager loading of distant tables, but it should look something like that:

@tom = Tom.find(1, :include => {:dicks => [:tunnels]}, :order_by => "tunnels.length ASC")

That'll do it, but there's no need to use include, joins will do just
fine i.e.

@tom = Tom.find(1, :joins => {:dicks => :tunnels}, :order_by => "tunnels.length ASC")

Fred

Thanks very much. Final solution:

@tom = Tom.find(1, :joins => {:dicks => :tunnel}, :order => "tunnels.length ASC")

(tunnels singular).

Mike