4

我想使用chartkick 制作用户注册数量(每天)的折线图,它是一个简单的一行代码,但它给出了一个错误

`undefined method `group_by_day' for #<Class:0x00000004b4fbf0>`

在我看来我有

<%= line_chart User.group_by_day(:created_at).count %>

错误日志:

Rendered admin/dashboards/index.html.erb within layouts/application (145.2ms)
Completed 500 Internal Server Error in 1018ms

NoMethodError - undefined method `group_by_day' for #<Class:0x00000004b4fbf0>:
  activerecord (3.2.14) lib/active_record/dynamic_matchers.rb:55:in `method_missing'
  app/views/admin/dashboards/index.html.erb:38:in `_app_views_admin_dashboards_index_html_erb___3330517857505238097_39214860'
  actionpack (3.2.14) lib/action_view/template.rb:145:in `block in render'
  activesupport (3.2.14) lib/active_support/notifications.rb:125:in `instrument'
  actionpack (3.2.14) lib/action_view/template.rb:143:in `render'
  actionpack (3.2.14) lib/action_view/renderer/template_renderer.rb:48:in `block (2 levels) in render_template'
  actionpack (3.2.14) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
  activesupport (3.2.14) lib/active_support/notifications.rb:123:in `block in instrument'
  activesupport (3.2.14) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (3.2.14) lib/active_support/notifications.rb:123:in `instrument'
  actionpack (3.2.14) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
4

2 回答 2

7

要获得group_by_day支持,您需要捆绑另一个 gem:来自Chartkick同一作者的 groupdate。

于 2013-11-04T18:40:58.267 回答
2

我看不到group_by_day方法的定义。这不是标准方法。可能是你在引用group_by.

在任何情况下,group_by都没有为 ActiveRecord 类定义。它定义在array.

您首先必须将记录提取到一个数组中。另外,如果created_at是一个方法,你需要传递的是使用&。

User.all.group_by(&:created_at)

另外,我不确定.count最后的行为是否符合您的预期。它正在计算日期的组(在 created_at 的情况下,它会为每条记录返回几乎一个项目,因为 created_at 真的几乎不重复)

于 2013-11-04T18:02:38.637 回答