mongodb/mongoid · error · Mongoid::Errors::MixedRelations
Referencing a(n) document from the document via a…
Error message
Referencing a(n) %{embedded} document from the %{root} document via a non-embedded association is not allowed since the %{embedded} is embedded. What it means
raise_mixed fires when a referenced (non-embedded) association is given a target that is itself an embedded document. Persisting such a cross-root reference is impossible because embedded documents have no collection of their own.
Solutions
- Store a reference to the embedded document's root document instead
- Duplicate the needed data into a standalone referenced document
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at lib/mongoid/association/proxy.rb:152 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/46446ec0d57ea643.
Report an issue: GitHub.
Appendix: source
Thrown at lib/mongoid/association/proxy.rb:152
# Whether the proxy can forward the method to the target.
#
# @param [ String | Symbol ] name The name of the method.
# @param [ Object... ] *args The +respond_to?+ arguments.
#
# @api private
ruby2_keywords def respond_to_missing?(name, *args)
_target.respond_to?(name, *args)
end
# When the base document illegally references an embedded document this
# error will get raised.
#
# @example Raise the error.
# relation.raise_mixed
#
# @raise [ Errors::MixedRelations ] The error.
def raise_mixed
raise Errors::MixedRelations.new(_base.class, _association.klass)
end
# When the base is not yet saved and the user calls create or create!
# on the association, this error will get raised.
#
# @example Raise the error.
# relation.raise_unsaved(post)
#
# @param [ Document ] doc The child document getting created.
#
# @raise [ Errors::UnsavedDocument ] The error.
def raise_unsaved(doc)
raise Errors::UnsavedDocument.new(_base, doc)
end
# Executes a callback method
#
# @example execute the before add callbackView on GitHub (pinned to 0da0e23d71)