开发者

order by foreign key in activerecord

开发者 https://www.devze.com 2022-12-08 16:31 出处:网络
I hav开发者_开发技巧e 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?

I hav开发者_开发技巧e 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


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


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')
0

精彩评论

暂无评论...
验证码 换一张
取 消