0

I have installed gem groupdate and gem chartkick and already restart my server. I'm enable to display the graph that sorted by group_by_day at my show.html.erb. I'm using postgresql database

show.html.erb

<%= @material.current_stock %>
<%= line_chart @material.group_by_day(:updated_at).sum(:current_stock) %>

controller/material_controller.rb

def show
   @material = Material.find(params[:id])
end

it's working fine when I declare using the class

<%= line_chart Material.group_by_day(:updated_at).sum(:current_stock) %>

but not when I replace Material with @material.

  • are you using `sqlite` DB? – uzaif Jul 23 '16 at 03:43
  • @uzaif, I'm using postgresql – hazimIskandar Jul 23 '16 at 03:46
  • There is a little Gotch with `Groupdate Gem`. In order to fix this I have to convert `created_at` to respective time zone. For more info [My Question](http://stackoverflow.com/questions/36178726/rails-group-by-hour-of-the-day-for-created-at-column). And you have to call `group-by-day` on ActiveRecord class not on object. – Gupta Jul 23 '16 at 08:13

2 Answers2

0

It should be work with Class method and array ,

controller/material_controller.rb

def show
   @material = Material.where(id:params[:id])
end

Change with this your controller method

Atul Shukla
  • 171
  • 1
  • 8
0

The group_by_day works for either a whole class or a group of objects, there is no use to group a single @material object.

For the whole class you can use,

Material.group_by_day(:updated_at).sum(:current_stock)

For group of materials,

@materials = Material.where('your logic')

@materials.group_by_day { |u| u.updated_at }.sum(:current_stock)

But if you want to group for a single object try,

@material = Material.where('your logic').first

@material.group_by_day { |u| u.updated_at }.sum(:current_stock)
Sravan
  • 18,467
  • 3
  • 30
  • 54