# :dependent =\> :destroy

**URL:** <https://www.ruby-forum.com/t/dependent-destroy/54685>\
**Category:** Rails\
**Created:** [March 13, 2006, 1:15am UTC](https://www.ruby-forum.com/t/dependent-destroy/54685 "2006-03-13T01:15:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [March 13, 2006, 1:15am UTC](https://www.ruby-forum.com/t/dependent-destroy/54685/1 "2006-03-13T01:15:47Z")

</div>

Hi,

There is something I don’t understand about :dependent =\> :destroy. I  
hope someone can help me. Consider this code:

class Folder \< ActiveRecord::Base  
has\_many :myfiles  
has\_many :folders  
has\_many :group\_folders, :dependent =\> :destroy

validates\_uniqueness\_of :name, :scope =\> “folder\_id”  
validates\_presence\_of :name

before\_destroy :dont\_destroy\_folder\_with\_contents

# Folders containing files or sub-folders

# can not be deleted

def dont\_destroy\_folder\_with\_contents  
if self.folders.length \> 0 or self.myfiles.length \> 0  
raise “Can’t destroy folder with contents”  
end  
end  
end

It prevents folders with contents from being deleted, however the  
group\_folders do get deleted because of :dependent =\> :destroy. How can  
I prevent the group\_folders from being destroyed when a folder “has  
contents”? I do want the group\_folders to be destroyed when the folder  
is destroyed without a problem.

Thanks.

Mischa.

---

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [March 13, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/dependent-destroy/54685/2 "2006-03-13T22:20:46Z")

</div>

Anyone…?

---

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [March 13, 2006, 10:50pm UTC](https://www.ruby-forum.com/t/dependent-destroy/54685/3 "2006-03-13T22:50:13Z")

</div>

2006/3/12, Mischa B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> before\_destroy :dont\_destroy\_folder\_with\_contents
> 
> # Folders containing files or sub-folders
> 
> # can not be deleted
> 
> def dont\_destroy\_folder\_with\_contents  
> if self.folders.length \> 0 or self.myfiles.length \> 0  
> raise “Can’t destroy folder with contents”  
> end  
> end  
> end

Did you try returning false from here ? I think that is what you are  
supposed to do to prevent an action.

Hope that helps !

---

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [March 14, 2006, 1:02am UTC](https://www.ruby-forum.com/t/dependent-destroy/54685/4 "2006-03-14T01:02:42Z")

</div>

OK, someone on IRC helped me out.

This is the way to do it:

class Folder \< ActiveRecord::Base  
has\_many :myfiles  
has\_many :folders  
has\_many :group\_folders

before\_destroy :dont\_destroy\_folder\_with\_contents

# Folders containing files or sub-folders

# can not be deleted

def dont\_destroy\_folder\_with\_contents  
if self.folders.length \> 0 or self.myfiles.length \> 0  
raise “Can’t destroy folder with contents”  
end  
end

after\_destroy :destroy\_dependant\_group\_folders

# Delete dependant group\_folders

# This code should be executed after\_destroy

def destroy\_dependant\_group\_folders  
for group\_folder in self.group\_folders  
group\_folder.destroy  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [March 13, 2006, 11:04pm UTC](https://www.ruby-forum.com/t/dependent-destroy/54685/5 "2006-03-13T23:04:30Z")

</div>

> Did you try returning false from here ? I think that is what you are  
> supposed to do to prevent an action.

Yeah, tried that and the group\_folders still get deleted…

---

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [March 14, 2006, 1:09am UTC](https://www.ruby-forum.com/t/dependent-destroy/54685/6 "2006-03-14T01:09:01Z")

</div>

On 3/13/06, Mischa B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> after\_destroy :destroy\_dependant\_group\_folders
> 
> # Delete dependant group\_folders
> 
> # This code should be executed after\_destroy
> 
> def destroy\_dependant\_group\_folders  
> for group\_folder in self.group\_folders  
> group\_folder.destroy  
> end  
> end  
> end

But what if one or some of the group\_folder.destroy commands fail?

You could use a transaction to make sure that this entire destroy is  
an all or none change to the db.

-Peter

---

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [March 14, 2006, 11:45pm UTC](https://www.ruby-forum.com/t/dependent-destroy/54685/7 "2006-03-14T23:45:31Z")

</div>

> But what if one or some of the group\_folder.destroy commands fail?
> 
> You could use a transaction to make sure that this entire destroy is  
> an all or none change to the db.

Thanks for the suggestion, but I don’t think this is neccesary in my  
situation.
