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

The %{name} attribute can't be set to an instance of %{other

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

Error "The %{name} attribute can't be set to an instance of %{other_klass} as %{other_klass} has multiple associations referencing %{klass} as %{name}." thrown in mongodb/mongoid.

Source

Thrown at lib/mongoid/association/referenced/belongs_to/binding.rb:78

              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)
            inverses = _association.inverses(doc)
            return unless inverses.length > 1 && _base.send(_association.foreign_key).nil?

            raise Errors::InvalidSetPolymorphicRelation.new(
              _association.name, _base.class.name, _target.class.name
            )
          end
        end
      end
    end
  end
end

View on GitHub (pinned to 0da0e23d71)

When it happens

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