Rails / ActiveRecord sort by specific value

Is there any convenient way in Rails 3.2 to arrange an ActiveRecord relationship by moving records with a specific value for a specific field to the top of the relationship? For example, if MyModel has a country attribute, I would like to sort the relationships returned by any request, with entries that have country='Spain' before the front of the relationship.

+1
source share
1 answer

Something like that. Obviously, "name, identifier" will depend on your model and requirements.

 MyModel.order("country = 'Spain' DESC, name, id") 
+3
source

All Articles