Merge pull request #53 from raulpopadineti/speed-improvements-1

Speed up room's initial load by reducing N+1 queries
This commit is contained in:
Stanko Krtalić
2025-09-18 09:41:58 +02:00
committed by GitHub
2 changed files with 8 additions and 2 deletions

View File

@@ -32,7 +32,7 @@ class RoomsController < ApplicationController
end
def find_messages
messages = @room.messages.with_creator
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)

View File

@@ -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 || ""