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

Document reloading behavior for changed attributes

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 7.4.0
    • Affects Version/s: None
    • Component/s: Docs
    • Fully Compatible

      The reloading documentation (https://docs.mongodb.com/mongoid/master/reference/crud/#reloading) says:

      > Use the reload method to fetch the most recent version of a document from the database:

      However, in testing for MONGOID-5199 it appears that changed attributes are potentially remaining changed after reloading:

      class Band
        include Mongoid::Document
        embeds_one :label
      end
      
      class Label
        include Mongoid::Document
        field :name, type: String
        embedded_in :band
      end
      
      b = Band.create!
      
      Band.with_session do |s|
        s.start_transaction
      
        label = Label.new(name: "Neil")
        b.label = label
      
        b.reload
        puts b.label # <Label _id: 61bcab66d1327a521a5054a9, name: "Neil">
      
        s.commit_transaction
      end
      

      The following example maintains a changed value but somehow it's not the most recent one:

      Band.with_session do |s|
        s.start_transaction
      
        label = Label.new(name: "Neil")
        b = Band.new(label: label)
        b.save!
        b.label = nil
        b.label = label
        b.reload
        puts b.label # nil
      
        s.commit_transaction
      end
      

      The scope of this ticket is to investigate whether changed attributes are retained through reload and update the documentation accordingly.

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: