Order and limit with the amount of ActiveRecord?

I have this ActiveRecord amount:

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

I would like to limit it to the top 10 minutes. Can someone tell me the syntax for this?

Thanks in advance.

+3
source share
2 answers

You can :orderover the summarized column, and then :limitup to 10 rows:

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

Just add :limitfor example:

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

Source: https://habr.com/ru/post/1776311/


All Articles