From f28533bc47308284ea1574143719e6cd48e9f593 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Fri, 2 Jun 2023 11:11:55 +0530 Subject: [PATCH] feat(perf): update query to reduce N+1 impact [CW-1926] (#7228) * feat: update query to reduce N+1 impact * feat: remove channel eager loading fixes the error: ActiveRecord::EagerLoadPolymorphicError Exception: Cannot eagerly load the polymorphic association :channel * test: fix search test * chore: revert change --- app/finders/conversation_finder.rb | 6 +++--- .../v1/conversations/partials/_conversation.json.jbuilder | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/finders/conversation_finder.rb b/app/finders/conversation_finder.rb index 7ec0a6d30..cadbb4644 100644 --- a/app/finders/conversation_finder.rb +++ b/app/finders/conversation_finder.rb @@ -155,9 +155,9 @@ class ConversationFinder end def conversations - @conversations = @conversations.includes( - :taggings, :inbox, { assignee: { avatar_attachment: [:blob] } }, { contact: { avatar_attachment: [:blob] } }, :team, :contact_inbox - ) + @conversations = @conversations.includes(:taggings, :inbox, + { assignee: [{ account_users: [:account] }, { avatar_attachment: [:blob] }] }, + { contact: { avatar_attachment: [:blob] } }, :team, :contact_inbox, :messages) sort_by = SORT_OPTIONS[params[:sort_by]] || SORT_OPTIONS['latest'] @conversations.send(sort_by).page(current_page) end diff --git a/app/views/api/v1/conversations/partials/_conversation.json.jbuilder b/app/views/api/v1/conversations/partials/_conversation.json.jbuilder index 63e1f22fb..82b9caacf 100644 --- a/app/views/api/v1/conversations/partials/_conversation.json.jbuilder +++ b/app/views/api/v1/conversations/partials/_conversation.json.jbuilder @@ -22,7 +22,7 @@ if conversation.messages.first.blank? elsif conversation.unread_incoming_messages.count.zero? json.messages [conversation.messages.includes([{ attachments: [{ file_attachment: [:blob] }] }]).last.try(:push_event_data)] else - json.messages conversation.unread_messages.includes([:user, { attachments: [{ file_attachment: [:blob] }] }]).last(10).map(&:push_event_data) + json.messages conversation.unread_messages.includes([{ attachments: [{ file_attachment: [:blob] }] }]).last(10).map(&:push_event_data) end json.account_id conversation.account_id