mongodb/mongoid · error · ArgumentError

Attributes for nested association '#{association.name}' must

Error message

Attributes for nested association '#{association.name}' must be a Hash or an Array of key/value pairs.

What it means

Error "Attributes for nested association '#{association.name}' must be a Hash or an Array of key/value pairs." thrown in mongodb/mongoid.

Source

Thrown at lib/mongoid/association/nested/many.rb:40

        #   many.build(person)
        #
        # @param [ Document ] parent The parent document of the association.
        # @param [ Hash ] options The options.
        #
        # @return [ Array ] The attributes.
        def build(parent, options = {})
          @existing = parent.send(association.name)
          raise Errors::TooManyNestedAttributeRecords.new(existing, options[:limit]) if over_limit?(attributes)

          attributes.each do |attrs|
            if attrs.is_a?(::Hash)
              process_attributes(parent, attrs.with_indifferent_access)
            elsif attrs.is_a?(Array) && attrs.length > 1 && attrs[1].respond_to?(:with_indifferent_access)
              process_attributes(parent, attrs[1].with_indifferent_access)
            elsif attrs.is_a?(Array) && attrs.length.even?
              process_attributes(parent, Hash[*attrs].with_indifferent_access)
            else
              raise ArgumentError,
                    "Attributes for nested association '#{association.name}' must be a Hash or an Array of key/value pairs."
            end
          end
        end

        # Create the new builder for nested attributes on one-to-many
        # associations.
        #
        # @example Initialize the builder.
        #   Many.new(association, attributes, options)
        #
        # @param [ Mongoid::Association::Relatable ] association The association metadata.
        # @param [ Hash ] attributes The attributes hash to attempt to set.
        # @param [ Hash ] options The options defined.
        def initialize(association, attributes, options = {})
          @attributes = if attributes.respond_to?(:with_indifferent_access)
                          attributes.with_indifferent_access.sort do |a, b|
                            a[0].to_i <=> b[0].to_i

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/association/nested/many.rb:40 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/dcbae3df1b71517f. Report an issue: GitHub.