mongodb/mongoid · error · Mongoid::Errors::NoMetadata

Metadata not found for document of type

Error message

Metadata not found for document of type %{klass}.

What it means

Raised by association_name when the document carries no association metadata (_association is nil), i.e. it was not instantiated through any association. Mongoid can only derive an association name for documents that live inside one.

Solutions

  1. Instantiate the document through its association so metadata is set
  2. Call association_name only on documents where _association is present
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/mongoid/association.rb:104 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/37794aeb886c3a68. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/association.rb:104

    # @example Is the document in an embeds one?
    #   address.embedded_one?
    #
    # @return [ true | false ] True if in an embeds one.
    def embedded_one?
      _association && _association.is_a?(Association::Embedded::EmbedsOne)
    end

    # Get the association name for this document. If no association was defined
    #   an error will be raised.
    #
    # @example Get the association name.
    #   document.association_name
    #
    # @raise [ Errors::NoMetadata ] If no association metadata is present.
    #
    # @return [ Symbol ] The association name.
    def association_name
      raise Errors::NoMetadata.new(self.class.name) unless _association

      _association.name
    end

    # Determine if the document is part of an references_many association.
    #
    # @example Is the document in a references many?
    #   post.referenced_many?
    #
    # @return [ true | false ] True if in a references many.
    def referenced_many?
      _association && _association.is_a?(Association::Referenced::HasMany)
    end

    # Determine if the document is part of an references_one association.
    #
    # @example Is the document in a references one?
    #   address.referenced_one?

View on GitHub (pinned to 0da0e23d71)