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

.touch with custom field is broken for embedded documents

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.3.3
    • Affects Version/s: 7.3.0
    • Component/s: None
    • None
    • Fully Compatible

      Since https://jira.mongodb.org/browse/MONGOID-3468 .touch(:field) is broken for embedded documents. The touched timestamp is not persisted in the database for the embedded document. 

       

      This is because when a document is embedded, the persistence is delegated to the parent document, (here: https://github.com/mongodb/mongoid/blob/master/lib/mongoid/touchable.rb#L37 ) however a `field` argument is not given, and since it's nil the code that handles the atomic touch update here https://github.com/mongodb/mongoid/blob/2f3505c3641718b8fdbe2b8b81fc58dd517b29ee/lib/mongoid/atomic.rb#L384

      effectively removes the atomic operation for the embedded document.

       This is because the regex generated in this code does not handle the already passed update to a custom touch field.

       

       

       

      def touch_atomic_updates(field = nil)
        updates = atomic_updates
        return {} unless atomic_updates.key?("$set")
        touches = {}
        updates["$set"].each_pair do |key, value|
          key_regex = /updated_at|u_at#{"|" + field if field.present?}/
          touches.merge!({ key => value }) if key =~ key_regex
        end
        { "$set" => touches }
      end
      
      # the embedded document generated this atomic update
      # atomic_updates = {"$set"=>{"email_suppressions.0.notified_at"=>2010-10-10 10:00:00 UTC}}
      # because the parent is touched, the field is `nil`
      # the regex will be /updated_at|u_at/
      # this does not match the key =~ key_regex
      # the resulting $set will thus become: `{"$set"=>{}}`
      # effectively removing the atomic update for an embedded document
      # when the field is custom
      
      

       

       

       

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            robert@appsignal.com Robert Beekman
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: