我正在尝试在rails中执行这个假设简单的操作:
self.timesheets.select("sum(total) as total, sum(quantity) as quantity").first where self is a project
当我在控制台模式下执行此操作时,它可以工作,但它会呈现3列:
[#<Timesheet id: nil, quantity: 120.1, total: 6245.2>]
当我在应用程序中运行它时,收到此错误消息:
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'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract_adapter.rb:420:in `log'
activerecord (4.0.0) lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:885:in `select'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_all'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/query_cache.rb:61:in `block in select_all'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/query_cache.rb:76:in `cache_sql'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/query_cache.rb:61:in `select_all'
activerecord (4.0.0) lib/active_record/querying.rb:36:in `find_by_sql'
activerecord (4.0.0) lib/active_record/relation.rb:585:in `exec_queries'
activerecord (4.0.0) lib/active_record/relation.rb:471:in `load'
activerecord (4.0.0) lib/active_record/relation.rb:220:in `to_a'
activerecord (4.0.0) lib/active_record/relation/finder_methods.rb:325:in `find_first'
activerecord (4.0.0) lib/active_record/relation/finder_methods.rb:90:in `first'
activerecord-deprecated_finders (1.0.3) lib/active_record/deprecated_finders/relation.rb:129:in `first'
app/models/project.rb:27:in `recalc_spendings'
app/models/timesheet.rb:44:in `escalate_sums'
activesupport (4.0.0) lib/active_support/callbacks.rb:452:in `_run__1475279931726113629__save__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
activerecord (4.0.0) lib/active_record/callbacks.rb:299:in `create_or_update'
activerecord (4.0.0) lib/active_record/persistence.rb:106:in `save'
activerecord (4.0.0) lib/active_record/validations.rb:51:in `save'
activerecord (4.0.0) lib/active_record/attribute_methods/dirty.rb:32:in `save'
activerecord (4.0.0) lib/active_record/transactions.rb:270:in `block (2 levels) in save'
activerecord (4.0.0) lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
activerecord (4.0.0) lib/active_record/transactions.rb:209:in `transaction'
activerecord (4.0.0) lib/active_record/transactions.rb:323:in `with_transaction_returning_status'
activerecord (4.0.0) lib/active_record/transactions.rb:270:in `block in save'
activerecord (4.0.0) lib/active_record/transactions.rb:281:in `rollback_active_record_state!'
activerecord (4.0.0) lib/active_record/transactions.rb:269:in `save'
app/controllers/timesheets_controller.rb:73:in `block in create'
actionpack (4.0.0) lib/action_controller/metal/mime_responds.rb:363:in `retrieve_collector_from_mimes'
actionpack (4.0.0) lib/action_controller/metal/mime_responds.rb:189:in `respond_to'
app/controllers/timesheets_controller.rb:72:in `create'
actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.0) lib/active_support/callbacks.rb:463:in `_run__3822865716334379624__process_action__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (4.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.0) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call'
newrelic_rpm (3.6.9.171) lib/new_relic/rack/error_collector.rb:50:in `call'
newrelic_rpm (3.6.9.171) lib/new_relic/rack/agent_hooks.rb:28:in `call'
newrelic_rpm (3.6.9.171) lib/new_relic/rack/browser_monitoring.rb:23:in `call'
newrelic_rpm (3.6.9.171) lib/new_relic/rack/developer_mode.rb:42:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
remotipart (1.0.5) lib/remotipart/middleware.rb:30:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__1689713881852047757__call__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
better_errors (1.0.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (1.0.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (1.0.1) lib/better_errors/middleware.rb:56:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets'
actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
thin (1.6.1) lib/thin/connection.rb:82:in `block in pre_process'
thin (1.6.1) lib/thin/connection.rb:80:in `pre_process'
thin (1.6.1) lib/thin/connection.rb:55:in `process'
thin (1.6.1) lib/thin/connection.rb:41:in `receive_data'
eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
thin (1.6.1) lib/thin/backends/base.rb:73:in `start'
thin (1.6.1) lib/thin/server.rb:162:in `start'
rack (1.5.2) lib/rack/handler/thin.rb:16:in `run'
rack (1.5.2) lib/rack/server.rb:264:in `start'
railties (4.0.0) lib/rails/commands/server.rb:84:in `start'
railties (4.0.0) lib/rails/commands.rb:78:in `block in <top (required)>'
railties (4.0.0) lib/rails/commands.rb:73:in `<top (required)>'
bin/rails:4:in `<top (required)>'
ruby-debug-ide (0.4.22) lib/ruby-debug-ide.rb:86:in `debug_program'
ruby-debug-ide (0.4.22) bin/rdebug-ide:110:in `<top (required)>'
-e:1:in `<main>'
我对此消息并不感到惊讶,因为id已添加到query'result。
但为什么它是由activerecord添加的,我该如何解决?
谢谢!
修改
感谢PriteshJ,我理解我的查询结尾处添加的“第一个”语句包含了ID,告诉activerecord它是单个记录,而不是集合。
所以我知道我必须摆脱第一个条款但是当我这样做时,我收到另一个错误信息:
NoMethodError: undefined method `total' for #<ActiveRecord::Relation::ActiveRecord_Relation_Timesheet:0x007fe73da50470>
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:121:in `method_missing'
这是正常的,因为它正在调查关系而不是单个记录,但我不知道如何解决它。
虽然应该很简单......
答案 0 :(得分:5)
看起来您按timesheets.id
订购结果,或者您有一些默认订单范围或您正在添加订单的地方。
删除排序或添加要选择的查询列。
<强>更新强>
尝试使用
self.timesheets.select("sum(total) as total, sum(quantity) as quantity").to_a.first
activerecord中的 .first
将尝试对查询进行排序并选择第一个元素,因此首先使用.to_a
先获取结果,然后执行.first
答案 1 :(得分:2)
我认为select
不是你想要使用的方法,因为你实际上没有在数据库中选择一行。
也许sum
就是你想要的(如果我理解正确的话)。
这对你有用吗?:
total = project.timesheets.sum(:total)
quantity = project.timesheets.sum(:quantity)
答案 2 :(得分:0)
self.timesheets.pluck("sum(total)", "sum(quantity)")
答案 3 :(得分:0)
如果您使用select
,它将尝试使用您指定的字段构建模型。你最好使用jackpipe建议的pluck
。但是,我会进行一次更改 - 如果没有记录,像sum
这样的方法将返回0,但是pluck将返回nil,所以为了克服这个问题,你可以试试这个:
total, quantity = self.timesheets.pluck("sum(total)", "sum(quantity)").first.map(&:to_i)