# Understanding :dependent =\> :delete\_all?

**URL:** <https://www.ruby-forum.com/t/understanding-dependent-delete-all/83033>\
**Category:** Rails\
**Created:** [January 23, 2007, 6:00am UTC](https://www.ruby-forum.com/t/understanding-dependent-delete-all/83033 "2007-01-23T06:00:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmacgregor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9a140/32.png) [@gmacgregor](https://www.ruby-forum.com/u/gmacgregor)\
**Post date:** [January 23, 2007, 6:00am UTC](https://www.ruby-forum.com/t/understanding-dependent-delete-all/83033/1 "2007-01-23T06:00:51Z")

</div>

I can’t see what’s wrong here but that’s probably just because I’m so  
new to Rails! To get right into it… I have 4 tables that look like  
this:

Sites

- id

Projects

- id
- site\_id
- user\_id

Tasks

- id
- project\_id
- user\_id

Users

- id

The corresponding models:

class Site \< ARB  
has\_many :projects, :dependent =\> :delete\_all  
end

class Project \< ARB  
belongs\_to :site  
belongs\_to :user  
has\_many :tasks, :dependent =\> :delete\_all  
end

class Task \< ARB  
belongs\_to :project  
has\_one :user  
end

class User \< ARB  
has\_many :tasks  
end

The idea is:

1. If you kill a site you kill all of its projects (this works)
2. If you kill a project you kill all of its tasks (this works if you  
JUST delete a project but not if you delete an entire site)

I created a single site, then 4 projects and 5 tasks. I then deleted  
the site. All of the projects were destroyed, but none of the tasks  
were!? What am I doing wrong here?

My controller call to delete a site is pretty rudimentary:  
Site.find(params[:id]).destroy

Ideas? Thanks for the help!

Greg

---

<div class="post-metadata">

**Author:** ![gmacgregor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9a140/32.png) [@gmacgregor](https://www.ruby-forum.com/u/gmacgregor)\
**Post date:** [January 23, 2007, 10:34am UTC](https://www.ruby-forum.com/t/understanding-dependent-delete-all/83033/2 "2007-01-23T10:34:18Z")

</div>

If you use:

:dependent =\> :delete\_all

Then AR will just issue one database command which will delete all  
immediately dependent records.

If you use:

:dependent =\> :destroy

AR will instantiate every dependent record object and call its destroy  
method as well (which will honour its :dependent records as well). This  
is inefficient in that instead of one DELETE SQL statement for all  
records, there is one DELETE for every record destroyed but it will fix  
your problem.

If you are dealing with only a few records this will not usually be a  
problem but if you are dealing with millions then coding your cascading  
delete manually would probably be a better solution.

---

<div class="post-metadata">

**Author:** ![gmacgregor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9a140/32.png) [@gmacgregor](https://www.ruby-forum.com/u/gmacgregor)\
**Post date:** [January 23, 2007, 3:39pm UTC](https://www.ruby-forum.com/t/understanding-dependent-delete-all/83033/3 "2007-01-23T15:39:39Z")

</div>

Great thanks for the response Dave! Much appreciated!
