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

#delete causes dependent associations to be destroyed

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • 7.3.0
    • Affects Version/s: 7.2.1
    • Component/s: Associations
    • None
    • Environment:
      macos and linux
    • Minor Change

      A Mongoid::Document having associations that are marked `dependent: :destroy`

      Expected behavior

      • when calling delete only the record i want gets deleted

      Actual behavior

      • all associations are destroyed further cascading their associations to be destroyed

       

      Example Code to reproduce:

      ```
      class Test
      include Mongoid::Document
      has_many :testings, dependent: :destroy
      end

      class Testing
      include Mongoid::Document
      belongs_to :test
      before_destroy do |record|
      puts "delete must never call destroy that is what destroy is for!"
      throw(:abort)
      end
      end

      > test = Test.create
      > test.testings << Testing.create
      > test.testings.count
      1
      > test.delete
      delete must never call destroy that is what destroy is for!

      1. hackto achieve correct behavier
        class Test
        include Mongoid::Document
        has_many :testings, dependent: :destroy
        def delete
        self.class.where(id: self.id).delete_all
        end
        end
        ```

      This is contrary to what ActiveRecord does and because of that there is no means of deleting a single record with having control about if you want to delete instead destroy.

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            vitasek@tqsoft.de Ralf Vitasek
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: