我有这个ActiveRecord总和:
@websites = current_user.records.sum(:minutes, :group =>'website', :conditions => "website IS NOT NULL")
我想将它限制在10个最高分钟数。有人能让我知道它的语法吗?
提前致谢。
答案 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)