diff --git a/lib/topic_query.rb b/lib/topic_query.rb index 48aa18f0142..7bef13f38fc 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -693,8 +693,15 @@ class TopicQuery result = result.where.not(id: TopicTag.distinct.pluck(:topic_id)) end - if @options[:exclude_tag] && tag = Tag.find_by(name: @options[:exclude_tag]) - result = result.where.not(id: TopicTag.distinct.where(tag_id: tag.id).pluck(:topic_id)) + if @options[:exclude_tag].present? + result = result.where(<<~SQL, name: @options[:exclude_tag]) + topics.id NOT IN ( + SELECT topic_tags.topic_id + FROM topic_tags + INNER JOIN tags ON tags.id = topic_tags.tag_id + WHERE tags.name = :name + ) + SQL end end