Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `group_by_day' - rails 3.2

I want to make a line-chart of the number of User signups (per day) using chartkick, it is a simple one line code but it is giving an error

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

in my views I have

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

error logs:

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'
like image 840
ben Avatar asked Jan 13 '23 00:01

ben


2 Answers

To have group_by_day supported, you need to bundle another gem: groupdate from the same author of Chartkick.

like image 192
Mifeng Avatar answered Jan 23 '23 17:01

Mifeng


I can't see the definition of the group_by_day method. This is not a standard method. May be you are referencing group_by.

In any case, group_by is not defined for the ActiveRecord class. It's defined on array.

You first have to fetch the record into an Array. Also, if created_at is a method, you need to pass is using the &.

User.all.group_by(&:created_at)

Also, I'm not sure the .count at the end behaves as you may expect. It's counting the groups of the dates (and in case of created_at it will return almost one item for each record because the created_at is really hardly duplicated)

like image 22
Simone Carletti Avatar answered Jan 23 '23 16:01

Simone Carletti