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

destroy_all with conditions argument constrains the embedded documents being destroyed with their full attribute tree

    • Minor Change

      when executing `destroy_all` on an embedded association, the `$pullAll` operator might not work if the content of the relation was changed. the change might be just order of the values in one of the embedded objects, which will cause the operation to not delete anything.

       

      for example, say I have the following models

       

      class Parent
         include Mongoid::Document
         embeds_many :blocks
      end
      
      class Block
         include Mongoid::Document
         field :name, type: String
         embeds_many :children
      end
      
      class Child
         include Mongoid::Document
         embedded_in :block
        
         field :size, type: Integer
         field :order, type: Integer
         field :t
      end
      
      Mongo::Logger.logger=Logger.new(STDERR)
      Mongo::Logger.logger.level=Logger::DEBUG
      
      
      parent = Parent.new
      parent.blocks << Block.new(name: 'test', children: [size: 1, order: 1])
      parent.save!
      

       

      Now, I'll update the attributes of the child, switching the attributes order

       

      # this is just an example
      parent.blocks[0].children[0].assign_attributes(size: 2)
      

       

      now, a `$pullAll` command will be generate with an element such as `blocks: [\{_id: ObjectId(...), order: 1, size: 1}]` however, mongo still has the object as `{size: 1, order: 1}`, and will fail to find it and delete it

      parent.blocks.destroy_all(:name => 'test')
      

       

       why not using a `$pull` command and query it by the `_id` of the matched elements?

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            dvir@honeybook.com Dvir Cohen
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: