轨道/ ActiveRecord的排序特定值 [英] Rails/ActiveRecord sort by particular value

查看:108
本文介绍了轨道/ ActiveRecord的排序特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有用Rails 3.2任何方便的方式来订购ActiveRecord的关系,通过移动的记录与特定值特定领域的关系的前面?例如,如果为MyModel 有一个属性国家,我想排序的任何查询具有返回记录的关系国家='西班牙'的关系的前面。

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")

这篇关于轨道/ ActiveRecord的排序特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆