order and limit with ActiveRecord sum?
I have this ActiveRecord sum:
@websites = current_user.records.sum(:minutes, :group =>'website', :conditions =开发者_如何转开发> "website IS NOT NULL")
I would like to limit it to the 10 highest minute sums. Could someone let me know the syntax for that?
Thanks in advance.
You can :order
by the summed column and then :limit
it to 10 rows like this:
@websites = current_user.records.sum(:minutes,
:group => 'website',
:conditions => 'website IS NOT NULL',
:order => 'SUM(minutes) DESC',
:limit => 10)
Just add a :limit
, like so:
current_user.records.sum(:minutes, :group => '', :conditions => '', :limit => num)
精彩评论