Improve behavior when autosave: true

XMLWordPrintableJSON

    • None
    • Fully Compatible
    • Ruby Drivers
    • Needed
    • Hide

      1. What would you like to communicate to the user about this feature?

      See PR description at https://github.com/mongodb/mongoid/pull/6134

      2. Would you like the user to see examples of the syntax and/or executable code and its output?

      3. Which versions of the driver/connector does this apply to?

      9.0.11, 9.1.0

      Show
      1. What would you like to communicate to the user about this feature? See PR description at https://github.com/mongodb/mongoid/pull/6134 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to? 9.0.11, 9.1.0
    • None
    • None
    • None
    • None
    • None
    • None

      Note that Mongoid 8 has improved this over Mongoid 7, but there may be an opportunity to further improve.

      # To test behaviour with autosaving too much
      # Results at the bottom, v6, v7, v8 all save too much though v7 is by far the worst
      # & is what prompted me to patch this in the first place
      
      $save_ctr = Hash.new {|h,k| h[k] = 0}
      
      class Table
        include Mongoid::Document
        include Mongoid::Timestamps::Updated
      
        has_many :rows, autosave: true
        after_save { $save_ctr[self.class] += 1 }
      end
      
      class Row
        include Mongoid::Document
        belongs_to :table
        has_many :cells, autosave: true
        after_save { $save_ctr[self.class] += 1 }
      end
      
      class Cell
        include Mongoid::Document
        belongs_to :row
        after_save { $save_ctr[self.class] += 1 }
      end
      
      table = Table.new
      10.times do
        row = table.rows.build
        10.times do
          row.cells.build
        end
      end
      table.save!
      
      # Reset counter
      $save_ctr = Hash.new {|h,k| h[k] = 0}
      
      table = Table.find(table.id)
      new_row = table.rows.build
      new_row.cells.build
      table.save
      
      # Patched v7-support: 1 table, 1 row, 1 cell
      #
      # v6.4.8 - 1 table, 2 row, 1 cell
      # v7.5.4 - 1 table, 11 row, 101 cell
      # v8.1.5 - 1 table, 11 row, 1 cell
      pp $save_ctr
      

            Assignee:
            Jamis Buck
            Reporter:
            Alex Bevilacqua
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: