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

Calculation is different by cache in memory for association document

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 7.0.0, 7.0.5
    • Component/s: Associations
    • None
    • Environment:
      MongoDB 3.4.20
      Ruby on Rails 5.1.7
    • None
    • None
    • None
    • None
    • None
    • None

       

      I have a problem after updating Mongoid version from 6.4.4 to 7.0.0

      I have encountered a case where the result of a calculation does not match with the sum of the fields of the associated document.

      example:

      class User
        include Mongoid::Document
        include Mongoid::Timestamps
      
        embeds_many :orders
      end
      
      class Order
        include Mongoid::Document
      
        field :amount, type: Integer
      
        embedded_in :user
      end

       

      when Mongoid 6.4.4

       

      user.orders.build(amount: 200)
      user.orders.sum(:amount)
      => 200
      
      user.orders.delete_all
      
      user.orders.build(amount: 500)
      user.orders.sum(:amount)
      => 500

      when Mongoid 7.0.0 ( also 7.0.5 ) 

       

      user.orders.build(amount: 200)
      user.orders.sum(:amount)
      => 200
      
      user.orders.delete_all
      
      user.orders.build(amount: 500)
      user.orders.sum(:amount)
      => 200

      Whether it was an intentional change or a bug could not be determined from the change log.

      However, it feels like a very big change, so I want the same behavior as the previous version if possible.

       

       

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            holygrail81@gmail.com Kuniaki Hori
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: