Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 530a795

Browse files
authored
FIX: Instruct AR that we want to use ai_summaries for filtering. (#927)
We use `includes` instead of `joins` because we want to eager-load summaries, avoiding an extra query when summarizing. However, Rails will complain unless you explicitly inform them you plan to use that inside a `WHERE` clause.
1 parent dcde94a commit 530a795

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

lib/summarization/entry_point.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ def inject_into(plugin)
2020
plugin.register_modifier(:topic_query_create_list_topics) do |topics, options|
2121
if Discourse.filters.include?(options[:filter]) && SiteSetting.ai_summarization_enabled &&
2222
SiteSetting.ai_summarize_max_hot_topics_gists_per_batch > 0
23-
topics.includes(:ai_summaries).where(
24-
"ai_summaries.id IS NULL OR ai_summaries.summary_type = ?",
25-
AiSummary.summary_types[:gist],
26-
)
23+
topics
24+
.includes(:ai_summaries)
25+
.where(
26+
"ai_summaries.id IS NULL OR ai_summaries.summary_type = ?",
27+
AiSummary.summary_types[:gist],
28+
)
29+
.references(:ai_summaries)
2730
else
2831
topics
2932
end

spec/lib/modules/summarization/entry_point_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,43 @@
8686

8787
expect(serialized[:ai_topic_gist]).to be_nil
8888
end
89+
90+
it "works when the topic has whispers" do
91+
SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"
92+
admin = Fabricate(:admin)
93+
group.add(admin)
94+
# We are testing a scenario where AR could get confused if we don't use `references`.
95+
96+
first = create_post(raw: "this is the first post", title: "super amazing title")
97+
98+
_whisper =
99+
create_post(
100+
topic_id: first.topic.id,
101+
post_type: Post.types[:whisper],
102+
raw: "this is a whispered reply",
103+
)
104+
105+
Fabricate(:topic_ai_gist, target: first.topic)
106+
topic_id = first.topic.id
107+
TopicUser.update_last_read(admin, topic_id, first.post_number, 1, 1)
108+
TopicUser.change(
109+
admin.id,
110+
topic_id,
111+
notification_level: TopicUser.notification_levels[:tracking],
112+
)
113+
114+
gist_topic = TopicQuery.new(admin).list_unread.topics.find { |t| t.id == topic_id }
115+
116+
serialized =
117+
TopicListItemSerializer.new(
118+
gist_topic,
119+
scope: Guardian.new(admin),
120+
root: false,
121+
filter: :hot,
122+
).as_json
123+
124+
expect(serialized[:ai_topic_gist]).to be_present
125+
end
89126
end
90127
end
91128
end

0 commit comments

Comments
 (0)