From c6f82783bae8b1d43afc0c00119c9ba5cb145e11 Mon Sep 17 00:00:00 2001 From: Pranav Date: Wed, 11 Mar 2026 07:31:46 -0700 Subject: [PATCH] chore: Remove message touch:true, use combined update query (#13770) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removes touch: true from the belongs_to :conversation association on Message and consolidates the conversation timestamp update into the existing set_conversation_activity callback. Previously, every message save triggered two separate UPDATE queries on the conversation — one from Rails' touch (updating updated_at) and another from set_conversation_activity (updating last_activity_at). This combines both into a single update_columns call, reducing write load on the conversations table on every message creation. ### What changed - Removed touch: true from belongs_to :conversation in Message - Added updated_at: created_at to the existing update_columns call in set_conversation_activity --- app/models/message.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/message.rb b/app/models/message.rb index cf03c9502..f98da83a6 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -125,7 +125,7 @@ class Message < ApplicationRecord belongs_to :account belongs_to :inbox - belongs_to :conversation, touch: true + belongs_to :conversation belongs_to :sender, polymorphic: true, optional: true has_many :attachments, dependent: :destroy, autosave: true, before_add: :validate_attachments_limit @@ -429,7 +429,7 @@ class Message < ApplicationRecord def set_conversation_activity # rubocop:disable Rails/SkipsModelValidations - conversation.update_columns(last_activity_at: created_at) + conversation.update_columns(last_activity_at: created_at, updated_at: Time.current) # rubocop:enable Rails/SkipsModelValidations end