开发者

order and limit with ActiveRecord sum?

开发者 https://www.devze.com 2023-01-26 04:10 出处:网络
I have this ActiveRecord sum: @websites = current_user.records.sum(:minutes, :group =>\'website\', :conditions =开发者_如何转开发> \"website IS NOT NULL\")

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

精彩评论

暂无评论...
验证码 换一张
取 消