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!
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.