From e89a834cdef10997b1e2ee6a5b49764ad6d0ab63 Mon Sep 17 00:00:00 2001 From: Raul Popadineti Date: Tue, 16 Sep 2025 13:41:07 +0300 Subject: [PATCH 1/2] Speed up room's initial load by reducing N+1 queries --- app/controllers/rooms_controller.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index ae5820e..ffe365f 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -32,7 +32,11 @@ class RoomsController < ApplicationController end def find_messages - messages = @room.messages.with_creator + messages = @room.messages.with_rich_text_body_and_embeds + .with_attached_attachment + .preload(creator: :avatar_attachment) + .includes(attachment_blob: :variant_records) + .includes(boosts: :booster) if show_first_message = messages.find_by(id: params[:message_id]) @messages = messages.page_around(show_first_message) From 03d1c45d9764a5e322329cb388688dd895a123d2 Mon Sep 17 00:00:00 2001 From: Raul Popadineti Date: Wed, 17 Sep 2025 13:39:30 +0300 Subject: [PATCH 2/2] Refactor message loading in RoomsController to use combined scopes - Simplified message queries in RoomsController#find_messages by replacing multiple includes and preloads with consolidated scopes: with_creator, with_attachment_details, and with_boosts. - Defined new scopes in Message model to handle rich text, attachments, and boosts associations for cleaner and more maintainable code. --- app/controllers/rooms_controller.rb | 6 +----- app/models/message.rb | 8 +++++++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index ffe365f..b2d8142 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -32,11 +32,7 @@ class RoomsController < ApplicationController end def find_messages - messages = @room.messages.with_rich_text_body_and_embeds - .with_attached_attachment - .preload(creator: :avatar_attachment) - .includes(attachment_blob: :variant_records) - .includes(boosts: :booster) + messages = @room.messages.with_creator.with_attachment_details.with_boosts if show_first_message = messages.find_by(id: params[:message_id]) @messages = messages.page_around(show_first_message) diff --git a/app/models/message.rb b/app/models/message.rb index aa5aa10..e607f6a 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -12,7 +12,13 @@ class Message < ApplicationRecord after_create_commit -> { room.receive(self) } scope :ordered, -> { order(:created_at) } - scope :with_creator, -> { includes(:creator) } + scope :with_creator, -> { preload(creator: :avatar_attachment) } + scope :with_attachment_details, -> { + with_rich_text_body_and_embeds + with_attached_attachment + .includes(attachment_blob: :variant_records) + } + scope :with_boosts, -> { includes(boosts: :booster) } def plain_text_body body.to_plain_text.presence || attachment&.filename&.to_s || ""