简体   繁体   中英

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.

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

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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