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

Values seem to be lost when using add_to_set on a non-existent attribute on a loaded instance

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.1.1, 7.0.7
    • Affects Version/s: 7.1.0
    • Component/s: Attributes
    • None

      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.

      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.

      I made a PR here that I hope can help:

      https://github.com/mongodb/mongoid/pull/4749

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            joshua.klina@gmail.com Joshua Klina
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: