instructure/canvas-lms · error

can't add participants if there are no messages

Error message

can't add participants if there are no messages

What it means

add_participants computes last_message_at from conversation_messages.human.first and raises if nil, because adding participants to a conversation with no human messages would produce participants with no valid last_message_at/basis.

Solutions

  1. Ensure the conversation has at least one live (human) message before adding participants; otherwise create a new conversation with a message
  2. Check conversation_messages.human.exists? before calling add_participants
  3. If data corruption removed messages unexpectedly, restore messages or mark the conversation deleted

Example fix

# before
conversation.add_participants(user, new_users)
# after
if conversation.conversation_messages.human.exists?
  conversation.add_participants(user, new_users)
else
  raise 'conversation has no messages to extend'
end
Defensive patterns

Strategy: validation

Validate before calling

raise 'conversation has no human messages' unless conversation.conversation_messages.human.exists?

Type guard

null

Try / catch

begin
  conversation.add_participants(user, users)
rescue RuntimeError => e
  raise e unless e.message.include?('no messages')
  # create a new conversation with an initial message instead
end

Prevention

When it happens

Trigger: Calling add_participants on a conversation whose human (non-deleted/non-ignored) messages have all been deleted, or that only contains non-human messages, so conversation_messages.human.first is nil.

Common situations: All messages in the thread deleted before adding participants; data where every message was removed by cleanup or user deletion; importing/merging conversations left messageless.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


AI-assisted analysis of instructure/canvas-lms@1c9f0bb801 (2026-09-15). Data as JSON: /api/errors/82072834dc862555. Report an issue: GitHub.

Appendix: source

Thrown at app/models/conversation.rb:187

    message = nil
    shard.activate do
      user_ids = users.map(&:id).uniq
      raise "can't add participants to a private conversation" if private?

      transaction do
        lock!
        user_ids -= conversation_participants.map(&:user_id)
        next if user_ids.empty?

        if options[:no_messages]
          bulk_insert_options = {
            workflow_state: "unread",
            message_count: 0,
            private_hash:
          }
        else
          last_message_at = conversation_messages.human.first.try(:created_at)
          raise "can't add participants if there are no messages" unless last_message_at

          num_messages = conversation_messages.human.size

          bulk_insert_options = {
            workflow_state: "unread",
            last_message_at:,
            message_count: num_messages,
            private_hash:
          }
        end

        Shard.partition_by_shard(user_ids) do |shard_user_ids|
          unless shard_user_ids.empty?
            shard_user_ids.sort!
            shard_user_ids.each_slice(1000) do |sliced_user_ids|
              User.where(id: sliced_user_ids).update_all(["unread_conversations_count = unread_conversations_count + 1, updated_at = ?", Time.now.utc])
            end
          end

View on GitHub (pinned to 1c9f0bb801)