0

I'm using the Impressionist gem to capture article views. A user can create an article and can view these in his dashboard. What I'm looking to do is to display a graph using Chartkick showing the amount of articles created by day, comments by day and most importantly for this question, the impressions per day of the users articles (and only his), so if the user has two impressions, per article, and has two articles, it'd show 4 in the chart for that day.

In my show action I have:

@articles = current_user.articles
@comments = @articles.comments

And in my view:

<%= area_chart [
   {name: "Articles", data: @articles.group_by_week(:created_at, format: "%B %d, %Y").count },
   {name: "Comments", data: @comments.group_by_week(:created_at, format: "%B %d, %Y").count },
   {name: "Impressions", data: @impressions.group_by_week(:created_at, format: "%B %d, %Y").count }
]%>

I've tried so many ways of doing this, collecting the ids of the impressions, but every time I do I get 'no block given'.. like so (and try not to laugh):

@article_impressions = []

@articles.each do |article|
  @impression = Impression.where(impressionable_id: article.id)
  if @impression.present?
    @article_impressions << [@impression.ids]
  end
end

I've also tried just taking the @impression into the hash but same again, 'no block given'. I'm almost 100% certain I'm doing this the wrong way and I'm being a bit of an idiot. If anyone could help, it'd be much appreciated!

Thanks.

Martin
  • 55
  • 1
  • 9
  • Can you post some of the stacktrace you get for this? – excid3 Jun 21 '17 at 21:00
  • I hope that's enough.. Slightly different from above but same idea. https://gist.github.com/MartinMcDermid/c925b58b730cc42dab51b982ca32ba94 – Martin Jun 21 '17 at 21:11
  • 1
    I'm going to guess that it would be because in two cases you're using a regular Array and not an ActiveRecord::Relation with groupdate. Your `@test` array and `@user_vehicle_impressions` arrays are probably both causing the issue and if so, you'll want to convert those over to SQL queries instead or just manually do the count yourself in Ruby. – excid3 Jun 22 '17 at 14:34

0 Answers0