Is there any convenient way in Rails 3.2 to order an ActiveRecord relation by moving records with a particular value for a particular field to the front of the relation? For instance, if MyModel
has an attribute country
, I would like to sort relations returned by any query with records having country='Spain'
to the front of the relation.
Something like this. Obviously 'name, id' would depend on your model and requirements.
MyModel.order("country = 'Spain' DESC, name, id")
来源:https://stackoverflow.com/questions/17073194/rails-activerecord-sort-by-particular-value