Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix MONGOID-4874 Values seem to be lost when using add_to_set on a no…
…n-existent attribute on a loaded instance (#4749) * Retain values added to sets of loaded instances It seems when an instance is loaded from a database, its `attributes` method returns an instance of `BSON::Document`. This causes some surprising behavior when we use `#add_to_set` on that loaded instance. It seems when a value is assigned to a `BSON::Document` instance, it gets modified: https://github.com/mongodb/bson-ruby/blob/master/lib/bson/document.rb#L87-L89 During modification, the attribute loses its reference to the original value, however the `add_to_set` method relies on this reference: https://github.com/mongodb/mongoid/blob/136ccbc140b25719f48ff5185bb471f90c063148/lib/mongoid/persistable/pushable.rb#L27-L31 Since that reference is broken, it no longer update the instance's attribute and the value is lost in memory, though still retained in the DB. This ensures that the reference remains intact. * expand test coverage * express the requirements more strictly Co-authored-by: Oleg Pudeyev <[email protected]>
- Loading branch information