Rails: Order with nulls last

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

    Adding arrays together will preserve order:

    @nonull = Photo.where("collection_id is not null").order("collection_id desc")
    @yesnull = Photo.where("collection_id is null")
    @wanted = @nonull+@yesnull
    

    http://www.ruby-doc.org/core/classes/Array.html#M000271

提交回复
热议问题