Destroying embeds_many document when _id is nil deletes all docs

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • 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
            Reporter:
            Neil Shweky (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: