mongodb/mongoid · error · Mongoid::Errors::InvalidSetPolymorphicRelation
The attribute can't be set to an instance of as has…
Error message
The %{name} attribute can't be set to an instance of %{other_klass} as %{other_klass} has multiple associations referencing %{klass} as %{name}. What it means
During bind_one of a polymorphic embedded_in, check_polymorphic_inverses! resolved more than one inverse on the parent class for the assigned document, so which parent association owns the child is ambiguous and the assignment is rejected.
Solutions
- Set the relation through a specific parent association instead of the ambiguous setter
- Define inverse_of or distinct as: names on the parent associations to disambiguate
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at lib/mongoid/association/embedded/embedded_in/binding.rb:64 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/b5bb12735bb4ad78.
Report an issue: GitHub.
Appendix: source
Thrown at lib/mongoid/association/embedded/embedded_in/binding.rb:64
end
end
end
private
# Check for problems with multiple inverse definitions.
#
# @api private
#
# @example Check for inverses errors.
# binding.check_inverses!(doc)
#
# @param [ Document ] doc The document to check.
def check_polymorphic_inverses!(doc)
return unless inverses = _association.inverses(doc)
return unless inverses.length > 1
raise Errors::InvalidSetPolymorphicRelation.new(
_association.name, _base.class.name, _target.class.name
)
end
end
end
end
end
end
View on GitHub (pinned to 0da0e23d71)