-
Type: Bug
-
Resolution: Fixed
-
Priority: Critical - P2
-
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!
- 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.