Skip to content

A platform for community discussion. Free, open, simple.

FIX: Preload all ancestors of sidebar categories (#26715)

Daniel Waterworthe2ceea881548
diff --git a/app/models/site.rb b/app/models/site.rb
index 03b435dede7..e5e08a3ffc2 100644
--- a/app/models/site.rb
+++ b/app/models/site.rb
@@ -111,12 +111,7 @@ class Site
if @guardian.authenticated?
sidebar_category_ids = @guardian.user.secured_sidebar_category_ids(@guardian)
preloaded_category_ids.concat(
- Category
- .secured(@guardian)
- .select(:parent_category_id)
- .distinct
- .where(id: sidebar_category_ids)
- .pluck(:parent_category_id),
+ Category.secured(@guardian).ancestors_of(sidebar_category_ids).pluck(:id),
)
preloaded_category_ids.concat(sidebar_category_ids)
end
diff --git a/spec/models/site_spec.rb b/spec/models/site_spec.rb
index cc23095c014..f2a77c56845 100644
--- a/spec/models/site_spec.rb
+++ b/spec/models/site_spec.rb
@@ -195,14 +195,17 @@ RSpec.describe Site do
expect(site.categories).to eq([])
end
- it "returns only sidebar categories and their parent categories" do
- parent_category = Fabricate(:category)
+ it "returns only sidebar categories and their ancestors" do
+ SiteSetting.max_category_nesting = 3
+ grandfather_category = Fabricate(:category)
+ parent_category = Fabricate(:category, parent_category: grandfather_category)
category.update!(parent_category: parent_category)
Fabricate(:category_sidebar_section_link, linkable: category, user: user)
site = Site.new(Guardian.new(user))
expect(site.categories.map { |c| c[:id] }).to contain_exactly(
+ grandfather_category.id,
parent_category.id,
category.id,
)