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

Defining a field named

Error message

Defining a field named '%{field}' is not allowed.

What it means

Raised by validate_field_name when a field is declared with a name that Mongoid does not allow — typically a name colliding with destructive/reserved methods. Fires at class definition time during Macro.validate.

Solutions

  1. Rename the field to something not present in Mongoid.destructive_fields
  2. Avoid names that conflict with existing Mongoid::Document instance methods
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/mongoid/fields/validators/macro.rb:66 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/dbb454f23662e789. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/fields/validators/macro.rb:66

          [ name, :"#{name}?", :"#{name}=" ].each do |n|
            raise Errors::InvalidRelation.new(klass, n) if Mongoid.destructive_fields.include?(n)
          end
        end

        # Determine if the field name is valid, if not raise an error.
        #
        # @example Check the field name.
        #   Macro.validate_field_name(Model, :name)
        #
        # @param [ Class ] klass The model class.
        # @param [ Symbol ] name The field name.
        #
        # @raise [ Errors::InvalidField ] If the name is not allowed.
        #
        # @api private
        def validate_field_name(klass, name)
          [ name, :"#{name}?", :"#{name}=" ].each do |n|
            raise Errors::InvalidField.new(klass, name, n) if Mongoid.destructive_fields.include?(n)
          end
        end

        private

        # Determine if the field name is unique, if not raise an error.
        #
        # @example Check the field name.
        #   Macro.validate_name_uniqueness(Model, :name, {})
        #
        # @param [ Class ] klass The model class.
        # @param [ Symbol ] name The field name.
        # @param [ Hash ] options The provided options.
        #
        # @raise [ Errors::InvalidField ] If the name is not allowed.
        #
        # @api private
        def validate_name_uniqueness(klass, name, options)

View on GitHub (pinned to 0da0e23d71)