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

Updated_at not updating correctly after touch then update

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 9.0.0
    • Affects Version/s: None
    • Component/s: None

      I noticed a bug when updating after touching while investigating MONGOID-5291...

      When performing calling #touch on a document and then subsequently updating the document, the updated_at is not updated on the second operation.

      Here is a simple reproduce case:

      Unable to find source-code formatter for language: ruby. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      class Band
        include Mongoid::Document
        include Mongoid::Timestamps
      
        field :name, type: String
      end
      
      Band.destroy_all
      
      Band.create!
      
      band = Band.first
      puts "BAND: #{band.updated_at}"
      # => BAND: 2022-09-29 18:59:01 -0400
      sleep 2
      band.touch
      puts "BAND: #{band.updated_at}"
      # => BAND: 2022-09-29 18:59:03 -0400
      sleep 2
      band.name = "Neil"
      band.save!
      puts "BAND: #{band.updated_at}"
      # => BAND: 2022-09-29 18:59:03 -0400 # WRONG this should have updated
      

      The solution to this problem is to not mark the updated_at attribute changed on calling #touch, since it's already been persisted. This is consistent with Rails.

            Assignee:
            dmitry.rybakov@mongodb.com Dmitry Rybakov
            Reporter:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: