mirror of
https://github.com/discourse/discourse.git
synced 2025-03-09 14:34:35 +00:00
remove N+1 queries
This commit is contained in:
parent
b27d4dcded
commit
d6ca23a75b
@ -1,5 +1,9 @@
|
|||||||
class Category < ActiveRecord::Base
|
class Category < ActiveRecord::Base
|
||||||
belongs_to :topic, dependent: :destroy
|
belongs_to :topic, dependent: :destroy
|
||||||
|
belongs_to :topic_only_relative_url,
|
||||||
|
select: "id, title",
|
||||||
|
class_name: "Topic",
|
||||||
|
foreign_key: "topic_id"
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
|
|
||||||
has_many :topics
|
has_many :topics
|
||||||
@ -43,7 +47,7 @@ class Category < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def topic_url
|
def topic_url
|
||||||
topic.try(:relative_url)
|
topic_only_relative_url.try(:relative_url)
|
||||||
end
|
end
|
||||||
|
|
||||||
before_save do
|
before_save do
|
||||||
|
@ -22,7 +22,7 @@ class Site
|
|||||||
end
|
end
|
||||||
|
|
||||||
def categories
|
def categories
|
||||||
Category.popular
|
Category.popular.includes(:topic_only_relative_url)
|
||||||
end
|
end
|
||||||
|
|
||||||
def archetypes
|
def archetypes
|
||||||
|
@ -196,7 +196,7 @@ class TopicQuery
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
result = result.listable_topics.includes(:category)
|
result = result.listable_topics.includes(:category => :topic_only_relative_url)
|
||||||
result = result.where('categories.name is null or categories.name <> ?', query_opts[:exclude_category]) if query_opts[:exclude_category]
|
result = result.where('categories.name is null or categories.name <> ?', query_opts[:exclude_category]) if query_opts[:exclude_category]
|
||||||
result = result.where('categories.name = ?', query_opts[:only_category]) if query_opts[:only_category]
|
result = result.where('categories.name = ?', query_opts[:only_category]) if query_opts[:only_category]
|
||||||
result = result.limit(page_size) unless query_opts[:limit] == false
|
result = result.limit(page_size) unless query_opts[:limit] == false
|
||||||
|
Loading…
x
Reference in New Issue
Block a user