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/referenced/has_many.rb:221

        private

        def default_foreign_key_field
          @default_foreign_key_field ||= "#{inverse}#{FOREIGN_KEY_SUFFIX}"
        end

        def polymorphic_inverses(_other)
          [ as ]
        end

        def determine_inverses(other)
          matches = (other || relation_class).relations.values.select do |rel|
            relation_complements.include?(rel.class) &&
              rel.relation_class_name == inverse_class_name
          end
          if matches.size > 1
            return [ default_inverse.name ] if default_inverse

            raise Errors::AmbiguousRelationship.new(relation_class, @owner_class, name, matches)
          end
          matches.collect { |m| m.name } unless matches.blank?
        end

        def default_primary_key
          PRIMARY_KEY_DEFAULT
        end

        def query_criteria(object, base)
          crit = klass.criteria
          crit = crit.apply_scope(scope)
          crit = crit.where(foreign_key => object)
          crit = with_polymorphic_criterion(crit, base)
          crit.association = self
          crit.parent_document = base
          with_ordering(crit)
        end

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/association/referenced/has_many.rb:221 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/978d08db9d2d66d5. Report an issue: GitHub.