开发者

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

开发者 https://www.devze.com 2023-03-01 15:24 出处:网络
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 != n

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

精彩评论

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