0

I've added a priority value to sort a list of objects in ascending order. The value is optional on records, and in the production app there are a lot of records that exist where priority is null.

When I sort the list, it returns all the nulls first -- the expected behaviour should be to include the nulls last, sorted by created_at desc.

@company.spark_sessions.order('priority asc, created_at desc')

Is there a nice, standard Rails way to return null records at the end of the list?

The top comment on a thread from five years ago recommended adding arrays together to preserve order, which I'm not fond of.

tadman
  • 208,517
  • 23
  • 234
  • 262
VegaStudios
  • 378
  • 1
  • 4
  • 22
  • 1
    You don't really need a Rails way, just a mysql way http://stackoverflow.com/questions/2051602/mysql-orderby-a-number-nulls-last – Eyeslandic Apr 26 '17 at 22:25
  • 1
    Marking as a duplicate since there is no polyglot "rails way" to do this. – max Apr 26 '17 at 22:49

0 Answers0