ActiveRecord总和的订单和限制?

时间:2010-11-22 20:57:22

标签: ruby-on-rails ruby activerecord

我有这个ActiveRecord总和:

@websites = current_user.records.sum(:minutes, :group =>'website', :conditions => "website IS NOT NULL")

我想将它限制在10个最高分钟数。有人能让我知道它的语法吗?

提前致谢。

2 个答案:

答案 0 :(得分:11)

您可以通过汇总列:order,然后将:limit添加到10行,如下所示:

@websites = current_user.records.sum(:minutes,
    :group => 'website',
    :conditions => 'website IS NOT NULL',
    :order => 'SUM(minutes) DESC',
    :limit => 10)

答案 1 :(得分:0)

只需添加:limit,就像这样:

current_user.records.sum(:minutes, :group => '', :conditions => '', :limit => num)