0

I am having trouble with getting the sum of all impressions for a specific object that relates to a user within a 24 hour span. Basically, I want to count the daily impressions for the current_user's posts.

I tried six or seven different approaches, but none work. My latest is the one below.

 def posts_daily_impressions_count(current_user)
  current_user.posts.sum(&:impressionist_count).group_by_day(:created_at)
 end
Johnny C
  • 121
  • 1
  • 1
  • 11
  • First of all, you did some syntax mistake`current_user.posts.sum(:impressionist_count)` and after count we can't do groupby. – Kaushlendra Tomar May 28 '18 at 04:50
  • When you remove .group_by_day(:created_at) -- It does work. But, I have no method for getting the count for 24 hours. They were supposed to add this as a feature for the gem. – Johnny C May 28 '18 at 05:12

2 Answers2

0
posts_daily_impressions_count = Post.joins(:User).where('users.id = ? AND posts.created_at between ? and ?', current_user.id,Date.today.beginning_of_day, Date.today.end_of_day).select('sum(posts.impressionist_count) as total_impressions').group('posts.created_at')]

It will results as array like this:-

=> [#<Post:0x007f8ba416f300 id: nil>,
 #<Post:0x007f8ba416f170 id: nil>,
 #<Post:0x007f8ba416ef90 id: nil>,
 #<Post:0x007f8ba416ee00 id: nil>,
 #<Post:0x007f8ba416ec70 id: nil>,
 #<Post:0x007f8ba416eae0 id: nil>,
 #<Post:0x007f8ba416e950 id: nil>]

and you can call alias column name on each individual element of results of array like this:-

posts_daily_impressions_count[0].total_impressions
=> 8
Anand
  • 6,457
  • 3
  • 12
  • 26
  • Is there noway of summing up all columns related to the posts for the day? I just need a basic size for the 24 hour span, that will reset every 24 hours. – Johnny C May 28 '18 at 05:09
  • @JohnnyC for the day mean for today? you want only for today? – Anand May 28 '18 at 05:12
  • Yes, just a daily count of impressions for all elements related to a model within a 24 hour period. – Johnny C May 28 '18 at 05:15
  • It works with chartkick graphs, but I cannot get a numerical value when I chain current_user.posts.impressions, etc for the day. – Johnny C May 28 '18 at 05:21
  • @JohnnyC for the day you can specify that posts.created_at between Date.today.beginning_of_day and Date.today.end_of_day. – Anand May 28 '18 at 05:28
  • @JohnnyC so far as i understand posts has many impressions and for that you can't call `current_user.posts.impressions` but you can call `current_user.posts[0].impressions` – Anand May 28 '18 at 05:32
  • @JohnnyC I think is that you wanted to get daily impression count of each posts, and hopefully this would work. – Anand May 28 '18 at 05:34
  • Oh. I see. Thank you for helping me. – Johnny C May 28 '18 at 05:34
  • 1
    @JohnnyC Feel free to accept/upvote answer if it helped you. thanks – Anand May 28 '18 at 05:36
0

You can solve this problem by tracking only the posts created within the day/month/year.

Create the scopes for the posts

  scope :today, -> {where("created_at >= ? AND created_at < ?", Time.now.beginning_of_day, Time.now.end_of_day)}
  scope :yesterday, -> {where("created_at >= ? AND created_at < ?", 1.day.ago.beginning_of_day, 1.day.ago.end_of_day)}
  scope :this_month, -> {where("created_at >= ? AND created_at < ?", Time.now.beginning_of_month, Time.now.end_of_month)}

Apply them before the sum method

<%= current_user.posts.today.sum(&:impressionist_count) %>

This will give you the impressions that were created within the span of time that you're focusing on.

Johnny C
  • 121
  • 1
  • 1
  • 11