开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜