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

foo.items.map(&:destroy) deletes half of the documents

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 7.0.2
    • Component/s: Associations
    • Labels:
      None
    • Environment:
      Rails 5.2.3, Ruby 2.5.3

      Using the minimal example

      # app/models/shopping_cart.rb
      class ShoppingCart
        include Mongoid::Document
      
        embeds_many :items
      end
      
      # app/models/item.rb
      class Item
        include Mongoid::Document
      
        embedded_in :shopping_cart
      end
      

      when I want to destroy all items in the shopping cart, it will only delete half of them unless I explicitly specify #all:

      2.5.3 :001 > cart = ShoppingCart.new
       => #<ShoppingCart _id: 5ce6a904a8f8432e27184e1b, > 
      2.5.3 :002 > 32.times { cart.items.new }
       => 32 
      2.5.3 :003 > cart.save!
       => true 
      2.5.3 :004 > cart.items.map(&:destroy!).uniq
       => [true] 
      2.5.3 :005 > cart.reload.items.count
       => 16 
      2.5.3 :006 > cart.items.map(&:destroy!).uniq
       => [true] 
      2.5.3 :007 > cart.reload.items.count
       => 8 
      2.5.3 :008 > cart.items.all.map(&:destroy!).uniq
       => [true] 
      2.5.3 :009 > cart.reload.items.count
       => 0 
      

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            larnold@givve.com Leo Arnold
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: