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
)
How would you do something like that?
Thanks!
Can you just do an orderby("collection_id")? The nils should get grouped together.
精彩评论