-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
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.
- is duplicated by
-
MONGOID-4734 Updating an association value to object instance when the instance has multiple inverses changes the wrong field
- Closed