Rails/ActiveRecord sort by particular value

与世无争的帅哥 提交于 2019-11-28 06:25:05

问题


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:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!