mongodb/mongoid · error · Mongoid::Errors::InvalidRelation
Defining an association named '%{name}' is not allowed.
Error message
Defining an association named '%{name}' is not allowed. What it means
Error "Defining an association named '%{name}' is not allowed." thrown in mongodb/mongoid.
Source
Thrown at lib/mongoid/fields/validators/macro.rb:49
# @param [ Symbol ] name The field name.
# @param [ Hash ] options The provided options.
def validate(klass, name, options)
validate_field_name(klass, name)
validate_name_uniqueness(klass, name, options)
validate_options(klass, name, options)
end
# Validate the association definition.
#
# @example Validate the association definition.
# Macro.validate(Model, :name)
#
# @param [ Class ] klass The model class.
# @param [ Symbol ] name The field name.
# @param [ Hash ] options The provided options.
def validate_relation(klass, name, _options = {})
[ 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)
endView on GitHub (pinned to 0da0e23d71)
When it happens
Trigger: Thrown at lib/mongoid/fields/validators/macro.rb:49 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/6a3c67a24b3cb7d2.
Report an issue: GitHub.