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

Attempted to set a value for

Error message

Attempted to set a value for '%{name}' which is not allowed on the model %{klass}.

What it means

process_attribute hit an attribute name the model does not define as a field or association writer and dynamic attributes are disabled (or field-permitting configuration excludes it). Mongoid rejects mass-assignment of unknown attributes instead of silently dropping them.

Solutions

  1. Declare the field on the model or enable dynamic attributes
  2. Strip the unknown keys from the attributes hash before assignment
  3. Align field-permitting configuration with the submitted attributes
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/mongoid/attributes/processing.rb:120 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/b89bf408e57e38cb. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/attributes/processing.rb:120

      # @return [ Hash ] The pending nested attributes in key/value pairs.
      def pending_nested
        @pending_nested ||= {}
      end

      # If the attribute is dynamic, add a field for it with a type of object
      # and then either way set the value.
      #
      # @example Process the attribute.
      #   document.process_attribute(name, value)
      #
      # @param [ Symbol ] name The name of the field.
      # @param [ Object ] value The value of the field.
      def process_attribute(name, value)
        if !respond_to?("#{name}=", true) && (store_as = aliased_fields.invert[name.to_s])
          name = store_as
        end
        responds = respond_to?("#{name}=", true)
        raise Errors::UnknownAttribute.new(self.class, name) unless responds

        send("#{name}=", value)
      end

      # Process all the pending nested attributes that needed to wait until
      # ids were set to fire off.
      #
      # @example Process the nested attributes.
      #   document.process_nested
      def process_nested
        pending_nested.each_pair do |name, value|
          send("#{name}=", value)
        end
      end

      # Process all the pending items, then clear them out.
      #
      # @example Process the pending items.

View on GitHub (pinned to 0da0e23d71)