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

Ambiguous associations %{candidates} defined on %{klass}.

Error message

Ambiguous associations %{candidates} defined on %{klass}.

What it means

Error "Ambiguous associations %{candidates} defined on %{klass}." thrown in mongodb/mongoid.

Source

Thrown at lib/mongoid/association/embedded/embeds_one.rb:147

          @owner_class.cyclic = true if cyclic?
          @owner_class.validates_associated(name) if validate?
        end

        def relation_complements
          @relation_complements ||= [ Embedded::EmbeddedIn ].freeze
        end

        def polymorphic_inverses(_other = nil)
          [ as ]
        end

        def determine_inverses(_other)
          matches = relation_class.relations.values.select do |rel|
            relation_complements.include?(rel.class) &&
              # https://jira.mongodb.org/browse/MONGOID-4882
              rel.relation_class_name.sub(/\A::/, '') == inverse_class_name
          end
          raise Errors::AmbiguousRelationship.new(relation_class, @owner_class, name, matches) if matches.size > 1

          matches.collect { |m| m.name } unless matches.blank?
        end
      end
    end
  end
end

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/association/embedded/embeds_one.rb:147 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/758269ce193cce3c. Report an issue: GitHub.