2

我正在开发一个 Rails 3.2 webapp,在这个应用程序中我有一个统计功能。我想生成一个输出,其中包含某个任务产生了多少收入(对于选定的用户),并在此日期范围内按天对总和进行分组。

我怎样才能做到这一点?我在 Task 模型上创建了一个类方法:

def self.earnings (params)
 earnings = group("date(created_at)")
 earnings = earnings.where("user_id = ?", params[:user_id]) if params[:user_id].present?
 earnings = earnings.completed.sum(:item_value).round
 earnings
end

在用户控制器中,我得到了这个方法调用:

def employees
 if params[:search]
    @statistics = Task.earnings(params)
 end
end

当我运行这个我得到这个错误:

ActiveRecord::StatementInvalid (PG::Error: ERROR:  column "tasks.created_at" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ...ser_id = '13') GROUP BY date(created_at) ORDER BY created_at...
                                                             ^
: SELECT SUM("tasks"."item_value") AS sum_item_value, date(created_at) AS date_created_at FROM "tasks"  WHERE "tasks"."account_id" = 4 AND "tasks"."status" = 'completed' AND (user_id = '13') GROUP BY date(created_at) ORDER BY created_at DESC):
4

0 回答 0