mongodb/mongoid · error · ArgumentError

A class which includes Mongoid::Document is expected

Error message

A class which includes Mongoid::Document is expected

What it means

Raised by Document#mongoid_document_check!, a generic validation helper called from Document#becomes, when the target class passed for the document type conversion does not include Mongoid::Document. Only Mongoid document classes can be become-targets; the faulting input is the ineligible class.

Solutions

  1. Pass a class that includes Mongoid::Document to becomes
  2. Ensure the target class is defined and includes Mongoid::Document before calling becomes
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/mongoid/document.rb:303 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/a96b53c87e152939. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/document.rb:303

      return unless attributes.key?(stored) || !relation.blank?

      if relation.nil?
        attributes.delete(stored)
      else
        attributes[stored] = relation.send(:as_attributes)
      end
    end

    # Checks that the given argument is an instance of `Mongoid::Document`.
    #
    # @param klass [ Class ] The class to test.
    #
    # @raise [ ArgumentError ] if the class does not include
    #   Mongoid::Document.
    def mongoid_document_check!(klass)
      return if klass.include?(Mongoid::Document)

      raise ArgumentError, 'A class which includes Mongoid::Document is expected'
    end

    # Constructs a hash representing the internal state of this object,
    # suitable for passing to #internal_state=.
    #
    # @return [ Hash ] the map of internal state values
    def internal_state
      {
        id: _id,
        changed_attributes: changed_attributes,
        errors: errors,
        new_record: new_record?,
        destroyed: destroyed?,
        discriminator_key_was: self.class.discriminator_value
      }
    end

    # Updates the value of the discriminator_key for this object, setting its

View on GitHub (pinned to 0da0e23d71)