# Destroy ordering with associations... reasoning?

**URL:** <https://www.ruby-forum.com/t/destroy-ordering-with-associations-reasoning/109069>\
**Category:** Rails\
**Created:** [July 11, 2007, 9:45am UTC](https://www.ruby-forum.com/t/destroy-ordering-with-associations-reasoning/109069 "2007-07-11T09:45:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrewkendall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b77776/32.png) [@andrewkendall](https://www.ruby-forum.com/u/andrewkendall)\
**Post date:** [July 11, 2007, 9:45am UTC](https://www.ruby-forum.com/t/destroy-ordering-with-associations-reasoning/109069/1 "2007-07-11T09:45:08Z")

</div>

Hi,

I’m having a problem understanding why rails calls the ‘before\_destroy’  
methods in the ordering as as follows (w/contrived ex)…

class User  
belongs\_to :group

def before\_destroy  
# don’t delete the user if they’re named ‘test’  
false if name == ‘test’  
end  
end

class Group  
has\_many :users, :dependent =\> :destroy

def before\_destroy  
# change all names to ‘bob’ so that we can make sure this group can  
be deleted  
User.update\_all “name = ‘bob’”  
end  
end

If I call destroy on the group…

Group.find(:first).destroy

The ordering of operations is…  
before\_destroy is called for every user in the group first  
before\_destroy is called for the group

To me, before\_destroy for the group should be called first to do any  
“prep” as I have in my example. The method is named for what it should  
be doing… call the method BEFORE the object is DESTROYed. According  
to the ordering I’m seeing, ‘before\_destroy’ is being called after the  
destroy process has already begun. So why is this not the case? The  
docs say this method is called before he destroy method is called; is  
this not correct?

Confused,  
Andrew

---

<div class="post-metadata">

**Author:** ![andrewkendall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b77776/32.png) [@andrewkendall](https://www.ruby-forum.com/u/andrewkendall)\
**Post date:** [July 11, 2007, 2:35pm UTC](https://www.ruby-forum.com/t/destroy-ordering-with-associations-reasoning/109069/2 "2007-07-11T14:35:40Z")

</div>

Hi Andrew,

Andrew K. wrote:

> If I call destroy on the group…
> 
> Group.find(:first).destroy
> 
> The ordering of operations is…  
> before\_destroy is called for every user in the group first  
> before\_destroy is called for the group

The order is driven by the relationships you’ve specified. The  
:dependent  
=\> destroy on Group tells Rails to destroy any User child records  
_before_  
trying to destroy User records. That’s a basic referential integrity  
constraint.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![andrewkendall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b77776/32.png) [@andrewkendall](https://www.ruby-forum.com/u/andrewkendall)\
**Post date:** [July 11, 2007, 9:01pm UTC](https://www.ruby-forum.com/t/destroy-ordering-with-associations-reasoning/109069/3 "2007-07-11T21:01:36Z")

</div>

Bill W. wrote:

> Hi Andrew,
> 
> Andrew K. wrote:
> 
> > If I call destroy on the group…
> > 
> > Group.find(:first).destroy
> > 
> > The ordering of operations is…  
> > before\_destroy is called for every user in the group first  
> > before\_destroy is called for the group
> 
> The order is driven by the relationships you’ve specified. The  
> :dependent  
> =\> destroy on Group tells Rails to destroy any User child records  
> _before_  
> trying to destroy User records. That’s a basic referential integrity  
> constraint.
> 
> hth,  
> Bill

So I suppose my solution is to remove the :dependent option and manually  
destroy all users in the groups before\_destroy callback after I’ve done  
the “prep”.

So this now…

class Group  
has\_many :users

def before\_destroy  
# change all names to ‘bob’ so that we can make sure this group can  
be deleted  
User.update\_all “name = ‘bob’”  
# destroy all users  
Users.destroy\_all  
end  
end

I guess I’m looking for a different callback that doesn’t exist that  
truly gets called before Model.destroy regardless of associations.

Thanks,  
Andrew
