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

Destroying embeds_many document when _id is nil deletes all docs

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None

      When you have multiple embedded documents and you try to destroy one of them, Mongoid destroys all documents with the same _id as the deleted document, assuming that _id is unique. If all the _ids are nil, then all of the documents get deleted. Additionally, documents are compared by their _ids, so if all of _ids are nil, the first document is always selected for deletion locally:

      Unable to find source-code formatter for language: ruby. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      class User
        include Mongoid::Document
        include Mongoid::Timestamps
      
        embeds_many :accounts
      end
      
      class Account
        include Mongoid::Document
      
        embedded_in :user
      end
      
      u = User.create!
      
      u.accounts << Account.new(_id: nil)
      u.accounts << Account.new(_id: nil)
      
      
      u.accounts[1].destroy
      
      p u.accounts.length
      # => 1 (correct)
      p u.accounts.first.num
      # => 2 (expected 1)
      p u.reload.accounts.size
      # => 0 (expecting 1)
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: