我正在开发一个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):