user94154
user94154

Reputation: 16564

order by foreign key in activerecord

I have a tables Foo and Bar. Foo has one Bar. When I query Foo, how can I order it by a date column in the Bar table?

Thanks

Upvotes: 4

Views: 3209

Answers (2)

Matthew Smith
Matthew Smith

Reputation: 6255

Refer to the ActiveRecord Query Interface page: http://guides.rubyonrails.org/active_record_querying.html#joining-tables

Note that sometimes a prefix is added to the table name so you may need to do something like:

Foo.all(:joins => :bar, :order => Bar.table_name + '.created_at')

Upvotes: 0

khelll
khelll

Reputation: 23990

Foo.find(:all,:joins=>:boo, :order=>'bars.created_at DESC' )

Upvotes: 1

Related Questions