Skip to content

Commit

Permalink
Merge pull request #37 from challengepost/optimize_impressionist_count
Browse files Browse the repository at this point in the history
Optimize impressionist_count query via arel count with distinct option
  • Loading branch information
sferik committed Mar 21, 2012
2 parents 6e60164 + fcfacdf commit 95125f4
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 5 deletions.
5 changes: 1 addition & 4 deletions app/models/impressionist/impressionable.rb
Expand Up @@ -26,10 +26,7 @@ def impressionable?
def impressionist_count(options={})
options.reverse_merge!(:filter=>:request_hash, :start_date=>nil, :end_date=>Time.now)
imps = options[:start_date].blank? ? impressions : impressions.where("created_at>=? and created_at<=?",options[:start_date],options[:end_date])
if options[:filter]!=:all
imps = imps.select(options[:filter]).group(options[:filter])
end
imps.all.size
options[:filter] == :all ? imps.count : imps.count(options[:filter], :distinct => true)
end

def update_counter_cache
Expand Down
2 changes: 1 addition & 1 deletion test_app/spec/models/counter_caching_spec.rb
Expand Up @@ -21,7 +21,7 @@
describe "#update_counter_cache" do
it "should update the counter cache column to reflect the correct number of impressions" do
lambda {
Impression.create(:impressionable_type => @widget.class.name, :impressionable_id => @widget.id)
@widget.impressions.create(:request_hash => 'abcd1234')
@widget.reload
}.should change(@widget, :impressions_count).from(0).to(1)
end
Expand Down

0 comments on commit 95125f4

Please sign in to comment.