Rails: Order with nulls last -
in rails app i've run issue couple times i'd know how other people solve:
i have records value optional, records have value , null column.
if order column on databases nulls sort first , on databases nulls sort last.
for instance, have photos may or may not belong collection, ie there photos collection_id=nil
, collection_id=1
etc.
if photo.order('collection_id desc)
on sqlite nulls last on postgresql nulls first.
is there nice, standard rails way handle , consistent performance across database?
adding arrays preserve order:
@nonull = photo.where("collection_id not null").order("collection_id desc") @yesnull = photo.where("collection_id null") @wanted = @nonull+@yesnull
Comments
Post a Comment