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

timeless leacks on embedded documents with timestamp

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 6.4.2
    • Component/s: Persistence
    • Ruby Drivers

      Context

      Using `Mongoid::Timestamps` in parent as well as embedded documents, requires to enable `cascaded_callbacks` to the relationship, provided that `parent.save` will also `touch` the `updated_at` date of the embedded docs.

       

       

      Mongoid::VERSION
      # => "6.4.2" 

       

      • After[ researching among issues and fixes|https://jira.mongodb.org/browse/MONGOID-3282?jql=project%20%3D%20%2212981%22%20AND%20text%20~%20%22timeless%22%20ORDER%20BY%20priority%20DESC%2C%20updated%20DESC], I could not see any related bug report.

      The issue

      Looking at the code, it seems that it makes sure that `cascadable_children` gets their own `timeless` (count increment). However, for some reason, this doesn't work as expected?

      Recreation

      The embedded document

      class Bar
        include Mongoid::Document
        include Mongoid::Timestamps
        embedded_in :foo, class_name: "Foo"
        
        field :completed, type: Boolean, default: false
        field :baz, type: String
      end 

      The root/parent document

      class Foo
        include Mongoid::Document
        include Mongoid::Timestamps
        
        embeds_many :bars, class_name: "Bar", cascade_callbacks: true
        
        field :completed, type: Boolean, default: false
      end 

      The test case

       

      foo = Foo.create({bars: [{baz: "baz"}]})
      foo.updated_at
      # => Fri, 07 Jun 2024 04:46:45.187964828 NZST +12:00 
      foo.bars.first.updated_at
      # => Fri, 07 Jun 2024 04:46:45.187867405 NZST +12:00
      • Observe how both dates are at 4.46 am NZ

      The above shows that cascaded_callbacks propagated the `updated_at` change to the embedded doc (`bar`).

       

      Now let's change content and persist with `timeless`:

       

      foo.changed?
      # => false
      
      foo.bars.first.completed = true
      foo.completed = true
      
      foo.changed?
      # => true
      
      foo.timeless.save
      
      foo.updated_at
      # => Fri, 07 Jun 2024 04:46:45.187964828 NZST +12:00
      foo.bars.first.updated_at
      # => Fri, 07 Jun 2024 04:51:08.430987097 NZST +12:00

      While the root document (`foo`) has actually preserved the `updated_at` date, the embeded doc got its timestamp updated.

       

       

       

       

        1. 5782.rb
          1.0 kB
        2. output.txt
          2 kB

            Assignee:
            Unassigned Unassigned
            Reporter:
            oscar@ecoportal.co.nz Oscar Segura
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: