Top 3 records ordered by a field in rails
-
07-07-2019 - |
Question
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.
Solution
Considering you have Notice ActiveRecord class, this should do:
Notice.find(:all, :limit => 3, :order => 'particularField')
OTHER TIPS
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!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow