Reputation: 725
In my application I have models Campaign
& Map
:
class Campaign < ActiveRecord::Base
has_many :maps, :dependent => :destroy
class Map < ActiveRecord::Base
is_impressionable :counter_cache => true, :unique => :request_hash
belongs_to :campaign, :counter_cache => true, touch: true
In my Map
model, I'm using is_impressionable
that comes with installing impressionist gem
& I have added counter_cache
as well that will update my impressions_count
for every visit (in my maps
table).
In my campaigns#show
view, Im trying to add a chart for maps impressions
by using morris.js (I have added all needed files in my application and can see the chart).
In my chart, I want to show impressions
on campaign.maps
, but I am getting wrong data into my chart.
So basically, go to my impressions
table, and sum
all visits that my campaign.maps
has on the giving day.
This is what I have until now:
module CampaignsHelper
def impressions_chart_data_campaign(maps)
(7.days.ago.to_date..Date.today).map do |date|
{
created_at: date,
impressions: Impression.where("date(created_at) =?", date).where(id: maps).size
}
end
end
end
My campaigns#show
view:
= content_tag :div, "", id: "impressions_charts_double", data: {impressions: impressions_chart_data_campaign(@campaign.maps)}
:javascript
Morris.Area({
element: 'impressions_charts_double',
data: $('#impressions_charts_double').data('impressions'),
xkey: 'created_at',
ykeys: ['impressions'],
labels: ['Impressions']
});
As mentioned I am getting completely wrong numbers. This is queries in my console:
I'm not sure What Im doing wrong.
Upvotes: 0
Views: 50
Reputation: 5690
This line looks suspect:
impressions: Impression.where("date(created_at) =?", date).where(id: maps).size
The where(id: maps)
is selecting on the id
column for impressions, whereas you presumably want to be selecting on impressionable_type
and impressionable_id
?
Upvotes: 2