ActiveRecordNotFound on wisper publish

70 Views Asked by At

We have a model ChatRoom which has many messages, the ChatRoom implements add_message and receives the parameters for the message along with a boolean notify which specifies whether we should send an email or not for the created message.

We publish :message_created after the message is created, however in our subscriber we receive an ActiveRecordNotFound error however there is an ID present meaning that the message is persisted in the database.

We have a couple potential fixes however we want to understand the problem and it's cause.

class ChatRoom < ApplicationRecord
  has_many :messages, -> { order(:created_at) }

  def add_message(args)
    content = args.fetch(:content)
    creator = args.fetch(:creator)
    notify = args.fetch(:notify, false)

    message = self.messages.create!(content: content, creator: creator)
    publish(:message_created, message_id: message.id, notify: notify)
  end
end
class MessageCreated::MailChatSubscriber < ApplicationSubscriber
  def message_created(args)
    message = Message.find(args.fetch(:message_id))
    notify = args.fetch(:notify)

    Messages::Organizers::SendChatResponseMail.call(
      message: message,
      notify: notify
    )
  end
end

The error is presented below: Couldn't find Message with 'id'=575565

0

There are 0 best solutions below