开发者

ActiveRecord: order by records that have an association first, then those that don't

I have an app with Photos. Some Photos belong_to a Collection, some don't.

I'd like to create a scope that would sort a query such that any photo that belongs_to a Collection (ie collection_id != nil) is shown first, and then all the rest of the photos (ie where collection_id == nil)

开发者_Python百科

How would you do something like that?

Thanks!


Can you just do an orderby("collection_id")? The nils should get grouped together.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜