-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
Classified
belongs_to :owner, class_name: 'User'
User
has_many :posts, class_name: 'Classified', inverse_of: :owner
has_and_belongs_to_many :starred, class_name: 'Classified', inverse_of: nil
Error thrown
Mongoid::Errors::AmbiguousRelationship (
Problem:
Ambiguous relations :posts, :starred defined on User.
Summary:
When Mongoid attempts to set an inverse document of a relation in memory, it needs to know which relation it belongs to. When setting :owner, Mongoid looked on the class Classified for a matching relation, but multiples were found that could potentially match: :posts, :starred.
Resolution:
On the :owner relation on Classified you must add an :inverse_of option to specify the exact relationship on User that is the opposite of :owner.):
Why is this an issue?
Mongoid should not attempt to set the inverse document for the relation 'starred' since we explicitly set it to nil, leaving Mongoid with only one relation to match.