Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-3465

AmbiguousRelationship error when ambiguity is not present in model

    • Type: Icon: Task Task
    • Resolution: Done
    • 4.0.0 final
    • 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.

            Assignee:
            durran Durran Jordan
            Reporter:
            motorstreak motorstreak
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: