# :dependent =\> :destroy and before\_destroy

**URL:** <https://www.ruby-forum.com/t/dependent-destroy-and-before-destroy/125296>\
**Category:** Rails\
**Created:** [January 8, 2008, 1:18am UTC](https://www.ruby-forum.com/t/dependent-destroy-and-before-destroy/125296 "2008-01-08T01:18:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/4da419/32.png) [@William\_P](https://www.ruby-forum.com/u/William_P)\
**Post date:** [January 8, 2008, 1:18am UTC](https://www.ruby-forum.com/t/dependent-destroy-and-before-destroy/125296/1 "2008-01-08T01:18:35Z")

</div>

I have a problem with one of my models. The parent object has 2  
polymorphic relationships both specifying :dependent =\> :destroy. This  
all works fine until you attempt to delete the parent and the parent’s  
before\_destroy callback returns false. When the parent’s before\_destroy  
callback returns false, the parent is not deleted, but the transaction  
is still committed so all of the objects from the polymorphic  
relationships are destroyed. This is obviously not desirable. I have  
verified that the call back is returning false. Is there something I am  
missing or is this just normal. I can remove the :dependent =\> :destroy  
and place the relevant code in my before\_destroy callback, but this  
seems to defeat the purpose of :dependent and would get really messy on  
objects with many dependencies.

Here is the abbreviated code in question:

class Location \< AbstractBase  
  
has\_one :address, :as =\> :addressed, :dependent =\> :destroy  
has\_one :contact, :as =\> :contactable, :dependent =\> :destroy  
  
before\_destroy :any\_dependent\_assets?  
  
end

class AbstractBase \< ActiveRecord::Base  
  
###########  
protected  
###########

#this is used as a filter to stop deletion if there are any dependent  
assets  
def any\_dependent\_assets?  
if self.respond\_to? :assets  
c = self.assets.count  
if c \> 0  
self.errors.add self.human\_name, “is still assigned to #{c}  
assets”  
logger.debug “we are returning false”  
return false  
end  
end  
logger.debug “we are returning true”  
true  
end  
end

Here is the log snippet from the transaction in question:

SQL (0.160060) BEGIN  
Address Columns (0.158866) SHOW FIELDS FROM addresses  
Address Load (0.163060) SELECT \* FROM addresses WHERE  
(addresses.addressed\_id = 1 AND addresses.addressed\_type = ‘Location’)  
LIMIT 1  
Address Destroy (0.159641)  
WHERE `id` = 266

Contact Columns (0.035453) SHOW FIELDS FROM contacts  
Contact Load (0.036147) SELECT \* FROM contacts WHERE  
(contacts.contactable\_id = 1 AND contacts.contactable\_type = ‘Location’)  
LIMIT 1  
Contact Destroy (0.031610)  
WHERE `id` = 224

Asset Columns (0.036145) SHOW FIELDS FROM assets  
SQL (0.029946) SELECT count(\*) AS count\_all FROM assets WHERE  
(assets.deleted\_at IS NULL OR assets.deleted\_at \> ‘2008-01-07 23:59:05’)  
AND (assets.location\_id = 1)  
we are returning false  
SQL (0.355231) COMMIT

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![William\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/4da419/32.png) [@William\_P](https://www.ruby-forum.com/u/William_P)\
**Post date:** [January 8, 2008, 1:25am UTC](https://www.ruby-forum.com/t/dependent-destroy-and-before-destroy/125296/2 "2008-01-08T01:25:01Z")

</div>

You can run this in a transaction block:  
@parent = Parent.find(params[:id])  
Parent.transaction do  
@parent.destroy  
end

if @parent.destroy raises any exception the entire transaction will be  
rolled back and none of the children will be deleted.

On Jan 8, 2008 10:48 AM, William P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> :dependent and would get really messy on objects with many dependencies.  
> end  
> if self.respond\_to? :assets  
> end  
> WHERE `id` = 266  
> assets.deleted\_at IS NULL OR assets.deleted\_at \> ‘2008-01-07 23:59:05’)
> 
> >

–  
Ryan B.  
[http://www.frozenplague.net](http://www.frozenplague.net)  
Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![William\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/4da419/32.png) [@William\_P](https://www.ruby-forum.com/u/William_P)\
**Post date:** [January 8, 2008, 1:48am UTC](https://www.ruby-forum.com/t/dependent-destroy-and-before-destroy/125296/3 "2008-01-08T01:48:58Z")

</div>

Hey Ryan, thank for the suggestion but that won’t help as the destroy is  
already wrapped in a transaction and the children are still deleted. I  
can get things to work if I raise an exception in the parent’s before  
filter and catch it in a begin / rescue / end block in the controller. I  
guess what I really want to know is why would :dependent children be  
destroyed if the parent’s before\_destroy filter returns false? Seems to  
me that is something is “dependent” than it should be just that. It  
would be deleted only if the parent can be. I was expecting to see the  
transaction rolled back when the parent’s before\_destroy callback  
returned false

-Bill

Ryan B. wrote:

> On Jan 8, 2008 10:48 AM, William P. \<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Is there something I am missing or is this just normal. I can  
> has\_one :contact, :as =\> :contactable, :dependent =\> :destroy
> 
> ```
> end
> Address Load (0.163060) SELECT * FROM addresses WHERE
> WHERE `id` = 224
> 
> ```
> 
> Ryan B.  
> [http://www.frozenplague.net](http://www.frozenplague.net)  
> Feel free to add me to MSN and/or GTalk as this email.
> 
> >

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![William\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/4da419/32.png) [@William\_P](https://www.ruby-forum.com/u/William_P)\
**Post date:** [January 8, 2008, 2:04am UTC](https://www.ruby-forum.com/t/dependent-destroy-and-before-destroy/125296/4 "2008-01-08T02:04:11Z")

</div>

I’m guessing you’re a little bit confused on precisely how the dependent  
destroy works, along with a before\_destroy.

1. You tell Rails to delete the parent record.
2. It checks for it’s associated records and their :dependent settings.
3. If it is dependent =\> :destroy then it will delete these records.
4. Processes before\_destroy on parent record.
5. Parent record is destroyed if before\_destroy returns true.

The transaction will not be cancelled if the before\_destroy returns  
false.  
Get it to raise an exception and it should roll it back.

On Jan 8, 2008 11:18 AM, William P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> -Bill  
> rolled back and none of the children will be deleted.
> 
> > destroyed. This is obviously not desirable. I have verified that the call  
> > has\_one :contact, :as =\> :contactable, :dependent =\> :destroy
> > 
> > ```
> > end
> > 
> > ```
> > 
> > Address Load (0.163060) SELECT \* FROM addresses WHERE (  
> > WHERE `id` = 224
> 
> Ryan B.
> 
> >

–  
Ryan B.  
[http://www.frozenplague.net](http://www.frozenplague.net)  
Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![William\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/4da419/32.png) [@William\_P](https://www.ruby-forum.com/u/William_P)\
**Post date:** [January 8, 2008, 2:10am UTC](https://www.ruby-forum.com/t/dependent-destroy-and-before-destroy/125296/5 "2008-01-08T02:10:39Z")

</div>

Ok, thats what I ended up with. Thanks again.

Ryan B. wrote:

> false. Get it to raise an exception and it should roll it back.  
> before\_destroy filter returns false? Seems to me that is something
> 
> > ```
> > @parent.destroy
> > 2 polymorphic relationships both specifying :dependent =>
> > purpose of :dependent and would get really messy on objects
> > <snip>
> > def any_dependent_assets?
> > true
> > Address Destroy (0.159641)  
> > SQL (0.029946) SELECT count(*) AS count_all FROM assets
> > 
> > ```
> 
> –  
> Ryan B.  
> [http://www.frozenplague.net](http://www.frozenplague.net)  
> Feel free to add me to MSN and/or GTalk as this email.
> 
> >

–  
Sincerely,

William P.
