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/belongs_to.rb:223

        end

        def polymorphic_inverses(other = nil)
          return unless other

          matches = other.relations.values.select do |rel|
            relation_complements.include?(rel.class) &&
              rel.as == name &&
              rel.relation_class_name == inverse_class_name
          end
          matches.collect { |m| m.name }
        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
          raise Errors::AmbiguousRelationship.new(relation_class, @owner_class, name, matches) if matches.size > 1

          matches.collect { |m| m.name }
        end

        # If set to true, then the associated object will be validated when this object is saved
        def require_association?
          required = @options[:required] if @options.key?(:required)
          required = !@options[:optional] if @options.key?(:optional) && required.nil?
          required.nil? ? Mongoid.belongs_to_required_by_default : required
        end

        def create_foreign_key_field!
          @owner_class.field(
            foreign_key,
            type: FOREIGN_KEY_FIELD_TYPE,
            identity: true,
            overwrite: true,
            association: self,

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/association/referenced/belongs_to.rb:223 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/4a95c3defa95793c. Report an issue: GitHub.