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

Match also on class name when looking up inverse.

    • Type: Icon: Task Task
    • Resolution: Done
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      Hit a snag when upgrading to Mongoid 3 with an app that makes good use of polymorphic models. Here's the setup:

      class User
        has_many :notifications, as: :interest
        has_many :icons, as: :interest
      end
      
      class Notification
        belongs_to :interest, polymorphic: true
      end
      
      class Icon
        belongs_to :interest, polymorphic: true
      end
      

      Setting icon.interest = user in Mongoid 3 causes an InvalidSetPolymorphicRelation exception. Mongoid is unhappy that User has two polymorphic relations called 'interest` and believes the assignment is ambiguous.

      However, the situation isn't ambiguous if we look at the relation classes. Because we're assigning from the Icon class, we know which interest on User the assignment pertains to.

      This patch adds logic to Metadata#lookup_inverses to only add a match when the inverse class and the potential match's class are equal.

      I added a spec w/ a simliar scenario to this that fails due to the InvalidSetPolymorphicRelation exception w/ the old code and passes with the patch. I also had to update one line in the metadata spec which, though it passed before, I believe was conceptually a hair off and broke w/ the patch (as a false negative).

            Assignee:
            Unassigned Unassigned
            Reporter:
            dzello Josh Dzielak
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: