0

I'm using Impressionist gem to capture impressions for records, and I'm using chartkick to graph the results. What I would like to do, is count the impressions for the current_user's records, and display the count on chartkick. Its a simple issue, but I cannot seem to figure out why the impressions are not being displayed on the graph. My code is below

analytics.html.erb

<%= line_chart current_user.posts.sum(&:impressionist_count).group_by_hour(:created_at).count, refresh: 60, xtitle: 'Hourly Posts Impression Count', ytitle: 'Amount of Views', label: 'View Count' %>
Cole Phiper
  • 349
  • 1
  • 13
  • Whats the result of `User.first.posts.group_by_hour(:created_at).impressionist_count.count`, or what ever user your testing on? – Sree Oct 09 '18 at 05:48
  • I get NoMethodError: undefined method 'first' and without, I get NoMethodError impressionist_count. When I use, user = User.find(2), user.posts.sum(&:impressionist_count). It works without issue. Attempting to use group_by_hour after the sum() method. It fails. – Cole Phiper Oct 09 '18 at 17:11

1 Answers1

0

I found a solution to this problem. Just in case anyone has it in the future. The code below, will inject the sum of the impressions into a hash, and then it will display correctly on the graph.

<%= line_chart current_user.posts.group_by_day(:created_at).count.inject(Hash.new(0)) { |acc, (k,v)| acc[:impressionist_count] += v; acc[k] = acc[:impressionist_count]; acc }.except(:impressionist_count) %>
Cole Phiper
  • 349
  • 1
  • 13