Edgars
Edgars

Reputation: 953

Get impresions count from today, yesterday and this month, Impresionist gem

I am using gem called impressionist to log page views on show action.

Everythink works just great.I can get number of all pageviews with:

@advertisement.impression_count

But now I want to be able filter pageviews per today, yesterday and this month.

So far I came up with this solution.

 @today  =  Impression.where( :conditions => { :created_at => Date.today...Date.today+1 }, :impresionable_id =>@advertisement.id)

There is no errors.

Then In view:

<%= "#{@today} views so far!" %>

gives me #<Impression::ActiveRecord_Relation:0x000000068d46f8>

then I tried to add like : <%= "#{@today.impression_count} views so far!" %> gives me this :

undefined method `impression_count'

then I tried just :<%= "#{@today.count} views so far!" %> and still error:

Mysql2::Error: Unknown column 'conditions.created_at' in 'where clause': SELECT COUNT(*) FROM `impressions`  WHERE (`conditions`.`created_at` >= '2014-12-18' AND `conditions`.`created_at` < '2014-12-19') AND `impressions`.`impresionable_id` = 127

Any ideas ?

Thanks in advance!

Upvotes: 0

Views: 570

Answers (4)

Edgars
Edgars

Reputation: 953

It was easier than I though

In advertisement.rb

 has_many :impressions, :as=>:impressionable


     def view_count_yesterday
       impressions.where("created_at >= ? AND created_at < ?",                 1.day.ago.beginning_of_day, 1.day.ago.end_of_day).size

     end

      def view_count_today
        impressions.where("created_at >= ? AND created_at < ?", Time.now.beginning_of_day, Time.now.end_of_day).size
               # impressionist_count(:start_date => 1.day.ago)
     end

Upvotes: 0

rmagnum2002
rmagnum2002

Reputation: 11421

Add scopes in impression.rb

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)}

in Controller:

 @today = Impression.today.where(impresionable_id: @advertisement.id)
 @yesterday = Impression.yesterday.where(impresionable_id: @advertisement.id)
 @this_month = Impression.this_month.where(impresionable_id: @advertisement.id)

And you can use these scopes anywhere you need to filter Impressions by date today, yesterday or this month. It's better compared to writing the where clause everywhere.

Upvotes: 2

SHS
SHS

Reputation: 7744

There's no need for the conditions hash.

today = Date.today
range = today..today.next_day
@imp = Impression.where(created_at: range, impressionable_id: @advertisement.id)

And if an @advertisement can have impressions, then the following would be better:

@imp = @advertisement.impressions.where(created_at: range)

Then to get the count, you must:

@today = @imp.count

Also, just FYI, you might need to use DateTime.now instead of Date.today because you're comparing with a datetime field i.e. created_at.

Upvotes: 1

dcarneiro
dcarneiro

Reputation: 7170

@today = Impression.where( :conditions => { :created_at => Date.today...Date.today+1 }, :impresionable_id =>@advertisement.id)

returns a #<Impression::ActiveRecord_Relation:0x000000068d46f8>.

Try this:

@today = Impression.where(created_at: Date.today...Date.today+1, impresionable_id: @advertisement.id).count

Upvotes: 2

Related Questions