class Message < ApplicationRecord belongs_to :chat has_many :tool_calls, dependent: :destroy enum :status, { pending: "pending", complete: "complete", failed: "failed" } validates :content, presence: true, allow_blank: true after_create_commit -> { broadcast_append_to chat, target: "messages" }, if: :broadcast? after_update_commit -> { broadcast_update_to chat }, if: :broadcast? scope :ordered, -> { order(created_at: :asc) } private def broadcast? raise NotImplementedError, "subclasses must set #broadcast?" end end