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

Improve behavior when autosave: true

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • 9.1.0
    • Affects Version/s: None
    • Component/s: Autosave
    • None
    • Ruby Drivers

      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:
            Unassigned Unassigned
            Reporter:
            alex.bevilacqua@mongodb.com Alex Bevilacqua
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: