Rails: Order with nulls last

后端 未结 10 1508
悲哀的现实
悲哀的现实 2020-11-30 19:12

In my Rails app I\'ve run into an issue a couple times that I\'d like to know how other people solve:

I have certain records where a value is optional, so some recor

10条回答
  •  悲哀的现实
    2020-11-30 19:54

    It seems like you'd have to do it in Ruby if you want consistent results across database types, as the database itself interprets whether or not the NULLS go at the front or end of the list.

    Photo.all.sort {|a, b| a.collection_id.to_i <=> b.collection_id.to_i}
    

    But that is not very efficient.

提交回复
热议问题