diff --git a/app/models/post.rb b/app/models/post.rb index 8324e40774f..a88b52da938 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -179,7 +179,7 @@ class Post < ActiveRecord::Base raw_links.each do |u| uri = URI.parse(u) host = uri.host - @linked_hosts[host] = (@linked_hosts[host] || 0) + 1 + @linked_hosts[host] ||= 1 end @linked_hosts end @@ -187,9 +187,11 @@ class Post < ActiveRecord::Base def total_hosts_usage hosts = linked_hosts.clone - # Count hosts in previous posts the user has made, PLUS these new ones - TopicLink.where(domain: hosts.keys, user_id: acting_user.id).each do |tl| - hosts[tl.domain] = (hosts[tl.domain] || 0) + 1 + TopicLink.where(domain: hosts.keys, user_id: acting_user.id) + .group(:domain, :post_id) + .count.keys.each do |tuple| + domain = tuple[0] + hosts[domain] = (hosts[domain] || 0) + 1 end hosts diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index f93f37cbf13..7b2b573919f 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -201,7 +201,7 @@ describe Post do end it "it counts properly with more than one link on the same host" do - three_links.linked_hosts.should == {"discourse.org" => 2, "www.imdb.com" => 1} + three_links.linked_hosts.should == {"discourse.org" => 1, "www.imdb.com" => 1} end end