mongodb/mongoid · error · ArgumentError

must specify a class or a document instance

Error message

must specify a class or a document instance

What it means

Error "must specify a class or a document instance" thrown in mongodb/mongoid.

Source

Thrown at lib/mongoid/persistence_context.rb:62

      @object = object
      set_options!(opts)
    end

    # Returns a new persistence context that is consistent with the given
    # child document, inheriting most appropriate settings.
    #
    # @param [ Mongoid::Document | Class ] document the child document
    #
    # @return [ PersistenceContext ] the new persistence context
    #
    # @api private
    def for_child(document)
      if document.is_a?(Class)
        return self if document == (@object.is_a?(Class) ? @object : @object.class)
      elsif document.is_a?(Mongoid::Document)
        return self if document.class == (@object.is_a?(Class) ? @object : @object.class)
      else
        raise ArgumentError, 'must specify a class or a document instance'
      end

      PersistenceContext.new(document, options.merge(document.storage_options))
    end

    # Get the collection for this persistence context.
    #
    # @example Get the collection for this persistence context.
    #   context.collection
    #
    # @param [ Object ] parent The parent object whose collection name is used
    #   instead of this persistence context's collection name.
    #
    # @return [ Mongo::Collection ] The collection for this persistence
    #   context.
    def collection(parent = nil)
      if parent
        parent.collection.with(client_options.except(:database, 'database'))

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/persistence_context.rb:62 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mongodb/mongoid@0da0e23d71 (2026-08-23). Data as JSON: /api/errors/b2cf71aa10fbdc98. Report an issue: GitHub.