Skip to content

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

FIX: Prevent discourse_reactions from overriding topic_view.posts preloads (#36041)

Natalie Tay1417b9828d7f
diff --git a/lib/topic_view.rb b/lib/topic_view.rb
index 944a7abd5de..cecea060c3f 100644
--- a/lib/topic_view.rb
+++ b/lib/topic_view.rb
@@ -929,9 +929,8 @@ class TopicView
:deleted_by,
:incoming_email,
:image_upload,
- :localizations,
)
-
+ @posts = @posts.includes(:localizations) if SiteSetting.content_localization_enabled
@posts = @posts.includes({ user: :user_status }) if SiteSetting.enable_user_status
@posts = apply_default_scope(@posts)
diff --git a/plugins/discourse-reactions/lib/discourse_reactions/posts_reaction_loader.rb b/plugins/discourse-reactions/lib/discourse_reactions/posts_reaction_loader.rb
index 7098a7cd5f0..79e88daebf4 100644
--- a/plugins/discourse-reactions/lib/discourse_reactions/posts_reaction_loader.rb
+++ b/plugins/discourse-reactions/lib/discourse_reactions/posts_reaction_loader.rb
@@ -3,7 +3,13 @@
module DiscourseReactions::PostsReactionLoader
def posts_with_reactions
if SiteSetting.discourse_reactions_enabled
- posts = object.posts.includes(:post_actions, reactions: { reaction_users: :user })
+ posts = object.posts
+
+ ActiveRecord::Associations::Preloader.new(
+ records: posts,
+ associations: [:post_actions, { reactions: { reaction_users: :user } }],
+ ).call
+
post_ids = posts.map(&:id).uniq
posts_reaction_users_count = TopicViewSerializer.posts_reaction_users_count(post_ids)
post_actions_with_reaction_users =
@@ -14,8 +20,6 @@ module DiscourseReactions::PostsReactionLoader
post.reaction_users_count = posts_reaction_users_count[post.id].to_i
post.post_actions_with_reaction_users = post_actions_with_reaction_users[post.id] || {}
end
-
- object.instance_variable_set(:@posts, posts)
end
end
end