FIX: N+1 query when tagging enabled and no tags in topic list query. Topic query ignored tags input when tagging is disabled.

This commit is contained in:
Neil Lalonde 2016-05-26 18:03:36 -04:00
parent 3d5716a2c8
commit 884779b5c1
2 changed files with 15 additions and 7 deletions

View File

@ -455,14 +455,18 @@ class TopicQuery
# ALL TAGS: something like this?
# Topic.joins(:tags).where('tags.name in (?)', @options[:tags]).group('topic_id').having('count(*)=?', @options[:tags].size).select('topic_id')
if @options[:tags] && @options[:tags].size > 0
result = result.joins(:tags).preload(:tags)
if SiteSetting.tagging_enabled
result = result.preload(:tags)
# ANY of the given tags:
if @options[:tags][0].is_a?(Integer)
result = result.where("tags.id in (?)", @options[:tags])
else
result = result.where("tags.name in (?)", @options[:tags])
if @options[:tags] && @options[:tags].size > 0
result = result.joins(:tags)
# ANY of the given tags:
if @options[:tags][0].is_a?(Integer)
result = result.where("tags.id in (?)", @options[:tags])
else
result = result.where("tags.name in (?)", @options[:tags])
end
end
end

View File

@ -118,6 +118,10 @@ describe TopicQuery do
let(:tag) { Fabricate(:tag) }
let(:other_tag) { Fabricate(:tag) }
before do
SiteSetting.tagging_enabled = true
end
it "returns topics with the tag when filtered to it" do
tagged_topic1 = Fabricate(:topic, {tags: [tag]})
tagged_topic2 = Fabricate(:topic, {tags: [other_tag]})