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

Destroying embedded doc via nested attributes _destroy should not raise DocumentNotFound

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Docs
    • None

      Currently, the following code raises Mongoid::Errors::DocumentNotFound

      class Person
        include Mongoid::Document
      
        embeds_many :phones
      
        accepts_nested_attributes_for :phones
      end
      
      class Phone
        include Mongoid::Document
      
        embedded_in :person
      end
      
      person = Person.create!
      person.update_attributes!(phones_attributes: [{ id: 'a' * 24, _destroy: true }])
      #=> raises Mongoid::Errors::DocumentNotFound

      It looks like this happens because the code first tries to "find" the embedded object (which raises error) before attempting to delete it.

      In this case, it would be much better to just ignore the non-existent embedded object, because it was probably already deleted. This seems to happen a lot in parts of the app where many users might be trying to modify the same object.

            Assignee:
            jamis.buck@mongodb.com Jamis Buck
            Reporter:
            shields@tablecheck.com Johnny Shields
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: