我试图在rails中做这个假设简单的操作:
self.timesheets.select("sum(total) as total, sum(quantity) as quantity").first where self is a project
Run Code Online (Sandbox Code Playgroud)
当我在控制台模式下执行它时,它可以工作,但它呈现3列:
[#<Timesheet id: nil, quantity: 120.1, total: 6245.2>]
Run Code Online (Sandbox Code Playgroud)
当我在应用程序中运行它时,我收到以下错误消息:
PG::GroupingError - ERROR: column "timesheets.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ..." WHERE "timesheets"."project_id" = $1 ORDER BY "timesheet...
^
:
activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:811:in `prepare_statement'
activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:772:in `exec_cache'
schema_plus (1.3.1) lib/schema_plus/active_record/connection_adapters/postgresql_adapter.rb:231:in `exec_cache_with_schema_plus'
activerecord (4.0.0) lib/active_record/connection_adapters/postgresql/database_statements.rb:139:in `block in exec_query'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract_adapter.rb:425:in `block in log'
activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument' …
Run Code Online (Sandbox Code Playgroud)