PERF: Cache serialized voters at topic view level (#28894)

This commit introduces a way to fetch the "serialized voters" for
multiple polls.

* Use a single query to fetch voters for all types of polls

* Refactor to introduce all_serialized_voters

* Cache serialized voters
This commit is contained in:
Bianca Nenciu
2024-09-18 12:01:40 +03:00
committed by GitHub
parent 02380af75c
commit dd5502f166
4 changed files with 70 additions and 131 deletions

View File

@@ -184,6 +184,7 @@ after_initialize do
if post_with_polls.present?
all_polls = Poll.includes(:poll_options).where(post_id: post_with_polls)
Poll.preload!(all_polls, user_id: @user&.id)
DiscoursePoll::Poll.preload_serialized_voters!(all_polls)
all_polls.each do |p|
polls[p.post_id] ||= []
polls[p.post_id] << p