2

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.

1 Answer 1

4

Something like this. Obviously 'name, id' would depend on your model and requirements.

MyModel.order("country = 'Spain' DESC, name, id")
Sign up to request clarification or add additional context in comments.

1 Comment

@RichardStokes It should move them all to the front. Can you share some code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.