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

Could not find metadata for association '%{name}' on model:

Error message

Could not find metadata for association '%{name}' on model: %{klass}.

What it means

Error "Could not find metadata for association '%{name}' on model: %{klass}." thrown in mongodb/mongoid.

Source

Thrown at lib/mongoid/attributes/nested.rb:54

        #
        # @option *args [ true | false ] :allow_destroy Can deletion occur?
        # @option *args [ Proc | Symbol ] :reject_if Block or symbol pointing
        #   to a class method to reject documents with.
        # @option *args [ Integer ] :limit The max number to create.
        # @option *args [ true | false ] :update_only Only update existing docs.
        # @option *args [ true | false ] :autosave Whether autosave should be enabled on the
        #   association. Note that since the default is true, setting autosave to nil will still
        #   enable it.
        def accepts_nested_attributes_for(*args)
          options = args.extract_options!.dup
          options[:autosave] = true if options[:autosave].nil?

          options[:reject_if] = REJECT_ALL_BLANK_PROC if options[:reject_if] == :all_blank
          args.each do |name|
            meth = "#{name}_attributes="
            nested_attributes["#{name}_attributes"] = meth
            association = relations[name.to_s]
            raise Errors::NestedAttributesMetadataNotFound.new(self, name) unless association

            autosave_nested_attributes(association) if options[:autosave]

            re_define_method(meth) do |attrs|
              _assigning do
                if association.polymorphic? and association.inverse_type
                  klass = association.resolver.model_for(send(association.inverse_type))
                  options = options.merge!(class_name: klass)
                end
                association.nested_builder(attrs, options).build(self)
              end
            end
          end
        end

        private

        # Add the autosave information for the nested association.

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/attributes/nested.rb:54 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/4b926688c972a44c. Report an issue: GitHub.