Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
784 views
in Technique[技术] by (71.8m points)

sql - Rails/ActiveRecord sort by particular value

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...