简体   繁体   English

Rails字段排序的前3条记录

[英]Top 3 records ordered by a field in rails

In ruby on rails how do I find the top 3 records of my table called notices ordered by a particular field, in my case I want to order by the position field which is an integer. 在ruby on rails中,我如何找到表的前3条记录,即按特定字段排序的通知,就我而言,我想按位置字段(整数)进行排序。

So my notices table looks like this: 所以我的通知表如下所示:

Any help would be greatly appreciated. 任何帮助将不胜感激。

考虑到您有Notice ActiveRecord类,应该这样做: Notice.find(:all, :limit => 3, :order => 'particularField')

You'd do something like: 您将执行以下操作:

Notice.find(:all,:order => "position", :limit => 3)

That would bring the 3 first records ordered by position (in this example, positions 1,2,3 or the first lesser ones. You can change the order value to "position DESC" if you want positions 20,19,18, for example). 这将使3个第一个记录按位置排序(在本示例中,位置1,2,3或第一个较小的记录。如果想要位置20,19,18,则可以将顺序值更改为“ position DESC” )。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM