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/embedded/embedded_in/binding.rb:64

              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)
            return unless inverses = _association.inverses(doc)
            return unless inverses.length > 1

            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/embedded/embedded_in/binding.rb:64 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/b5bb12735bb4ad78. Report an issue: GitHub.