# How do you figure out that a Parent model is trying to destroy it's children?

**URL:** <https://www.ruby-forum.com/t/how-do-you-figure-out-that-a-parent-model-is-trying-to-destroy-its-children/139157>\
**Category:** Rails\
**Created:** [May 31, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/how-do-you-figure-out-that-a-parent-model-is-trying-to-destroy-its-children/139157 "2008-05-31T17:30:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [May 31, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/how-do-you-figure-out-that-a-parent-model-is-trying-to-destroy-its-children/139157/1 "2008-05-31T17:30:18Z")

</div>

I have a simple acts\_as\_list model structure where the Language holds  
a lot of Wikis. I want to validate inside Wikis so as to not destroy  
the last Wiki in a Language. However, when the Language is destroyed,  
I want all the wikis associated with the Language to destroy  
themselves.

The solution seemed simple until I figured out that when I try to  
destroy a Language, the method fails at destroying the last Wiki.  
This is because I am trying to destroy the last Wiki in the Language.  
I need to, therefore, figure out a way to bypass the “last wiki  
validations” ONLY when I’m destroying a Language. Is there a  
conditional or a flag that lets me figure out that the parent is  
trying to destroy the children?

## Here’s my code, by the way:

class Language \< ActiveRecord::Base  
#indicate this is an array  
has\_many :wikis,  
:order =\> :position,  
:dependent =\> :destroy

#everything has to be unique!  
validates\_presence\_of :language  
validates\_presence\_of :url\_id  
validates\_uniqueness\_of :language  
validates\_uniqueness\_of :url\_id  
validates\_length\_of :url\_id,  
:in =\> 2…5  
validates\_format\_of :url\_id,  
:with =\> /^[a-z]{2}([a-z\_]{0,2}[a-z])?$/

def after\_update  
if self.id == 1 and self.url\_id != ‘en’  
raise “Cannot change this language’s url!”  
end  
end

## def after\_destroy if self.url\_id == ‘en’ raise “Cannot delete this language!” end end end

class Wiki \< ActiveRecord::Base  
#indicate this is array elements  
belongs\_to :language  
acts\_as\_list :scope =\> :language\_id

#general limits:  
validates\_presence\_of :title  
validates\_format\_of :title, :with =\> /^\w.+$/ #letter-first  
validates\_uniqueness\_of :title, :scope =\> :language\_id  
validates\_presence\_of :body  
validates\_length\_of :body, :minimum =\> 10

## def after\_destroy if self.id == 1 raise “Cannot delete this Wiki!” elsif ( Language.find( self.language\_id ) ).wikis.empty? raise “Cannot delete the last Wiki!” end end end

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [May 31, 2008, 8:14pm UTC](https://www.ruby-forum.com/t/how-do-you-figure-out-that-a-parent-model-is-trying-to-destroy-its-children/139157/2 "2008-05-31T20:14:18Z")

</div>

You can define what happens to dependents in your Language model…

has\_many :wikis, :dependent =\> :delete\_all

If you don’t want to :delete\_all, you could simply :nullify

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [May 31, 2008, 8:42pm UTC](https://www.ruby-forum.com/t/how-do-you-figure-out-that-a-parent-model-is-trying-to-destroy-its-children/139157/3 "2008-05-31T20:42:30Z")

</div>

Huh. I didn’t know delete bypasses the validations methods. Thank a  
lot!
