FIX: sort using ruby to avoid N+1 queries (#10915)

We are using preload to load tags into topics. When later we try to use `order` or `pluck` it is causing N+1

Usually, topics don't have many tags so sorting using ruby should be reasonably performant.
This commit is contained in:
Krzysztof Kotlarek 2020-10-14 18:20:41 +11:00 committed by GitHub
parent 2b5ca8af12
commit d77e31b7e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 4 deletions

View File

@ -10,10 +10,8 @@ module TopicTagsMixin
end
def tags
# Calling method `pluck` along with `includes` causing N+1 queries
order_setting = SiteSetting.tags_sort_alphabetically ? { name: :asc } : { topic_count: :desc }
tags = topic.tags.order(order_setting).map(&:name)
# Calling method `pluck` or `order` along with `includes` causing N+1 queries
tags = (SiteSetting.tags_sort_alphabetically ? topic.tags.sort_by(&:name) : topic.tags.sort_by(&:topic_count).reverse).map(&:name)
if scope.is_staff?
tags
else