Rails: Order with nulls last

后端 未结 10 1503
悲哀的现实
悲哀的现实 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:44

    Put minus sign in front of column_name and reverse the order direction. It works on mysql. More details

    Product.order('something_date ASC') # NULLS came first
    Product.order('-something_date DESC') # NULLS came last
    

提交回复
热议问题