diff --git a/lib/topic_view.rb b/lib/topic_view.rb index b10b2551e54..6a440eade50 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -212,6 +212,7 @@ class TopicView def post_counts_by_user @post_counts_by_user ||= Post.where(topic_id: @topic.id) + .where("user_id IS NOT NULL") .group(:user_id) .order("count_all DESC") .limit(24) diff --git a/spec/components/topic_view_spec.rb b/spec/components/topic_view_spec.rb index d59de8da21c..c10fc0128a2 100644 --- a/spec/components/topic_view_spec.rb +++ b/spec/components/topic_view_spec.rb @@ -158,6 +158,12 @@ describe TopicView do it 'returns the two posters with their counts' do expect(topic_view.post_counts_by_user.to_a).to match_array([[first_poster.id, 2], [coding_horror.id, 1]]) end + + it "doesn't return counts for posts with authors who have been deleted" do + p2.user_id = nil + p2.save! + expect(topic_view.post_counts_by_user.to_a).to match_array([[first_poster.id, 2]]) + end end context '.participants' do