我正在尝试创建一个图表,其中包含每天在我的网站上注册的用户数。
我使用:
User.group('date(created_at)').count
我正在关注this suggestion但我有一个错误:
@users.group("DATE_TRUNC('day', created_at)").count
(0.6ms) SELECT COUNT(*) AS count_all, DATE_TRUNC('day', created_at) AS date_trunc_day_created_at FROM "users" GROUP BY DATE_TRUNC('day', created_at) ORDER BY created_at DESC
PG::GroupingError: ERROR: column "users.created_at" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ... GROUP BY DATE_TRUNC('day', created_at) ORDER BY created_at...
^
: SELECT COUNT(*) AS count_all, DATE_TRUNC('day', created_at) AS date_trunc_day_created_at FROM "users" GROUP BY DATE_TRUNC('day', created_at) ORDER BY created_at DESC
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR: column "users.created_at" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ... GROUP BY DATE_TRUNC('day', created_at) ORDER BY created_at...
^
: SELECT COUNT(*) AS count_all, DATE_TRUNC('day', created_at) AS date_trunc_day_created_at FROM "users" GROUP BY DATE_TRUNC('day', created_at) ORDER BY created_at DESC
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:768:in `async_exec'
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:768:in `exec_no_cache'
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:138:in `block in exec_query'
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:in `block in log'
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:420:in `log'
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:885:in `select'
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_all'
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.0/lib/active_record/relation/calculations.rb:318:in `execute_grouped_calculation'
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.0/lib/active_record/relation/calculations.rb:222:in `perform_calculation'
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.0/lib/active_record/relation/calculations.rb:108:in `calculate'
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-deprecated_finders-1.0.3/lib/active_record/deprecated_finders/relation.rb:84:in `calculate'
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.0/lib/active_record/relation/calculations.rb:111:in `calculate'
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-deprecated_finders-1.0.3/lib/active_record/deprecated_finders/relation.rb:84:in `calculate'
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.0/lib/active_record/relation/calculations.rb:24:in `count'
from (irb):21
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /Users/scrivoaroby/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rUser.group('date(created_at)')
最佳答案
您是否在某处使用订单? (可能在范围内或更早的查询中。)
User.group('date(created_at)').count #=> should return a hash with {date => (no. of users)}
但是,就这一点而言,我们不知道 @users
是什么。因此,我建议您在这种情况下取消其范围,因为您只需要计数。
@users.unscoped.group("DATE_TRUNC('day', created_at)").count
关于ruby-on-rails - Activerecord 组不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23867686/