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

Multiple polymorphic embeds cause wrong association to be assigned

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 8.0.1
    • Affects Version/s: None
    • Component/s: Persistence
    • None
    • Minor Change

      Happy 2021 Mongoid team! I found another bug over the holidays.

      Suppose I have this class:

      class Customer
        embeds_one :home_address, class_name: 'Address', as: :addressable
        embeds_one :work_address, class_name: 'Address', as: :addressable
      end
      
      class Address
        embedded_in :addressable, polymorphic: true
      end

      In my app, strange things happen when I do:

       

      customer.work_address = Address.new

       

      I see cases of this assigning to home_address, which is very scary!

      I don't fully understand what's going on, but it appears that Mongoid gets tripped up assigning an "embedded_in" relationship in this case as there is ambiguity here.

       

      Reproduction:

      **I've reproduced this issue here: https://github.com/mongodb/mongoid/pull/4939

      As mentioned above, I see this issue in my app when doing `customer.work_address = Address.new`. However, so far I've only been able to reproduce it with:

       

      customer.work_address.addressable = customer
      

      If you study how the binding code works, this seems like a sufficient reproduction to get started.

       

       

      Proposed Behavior:

      Attempting to assign address.addressable = customer should throw an "Ambiguous assignment error" in this case. We should only be allowed to assign customer.home/work_address = address; not the other way around.

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            shields@tablecheck.com Johnny Shields
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: