# How to get rake task to delete dependents

**URL:** <https://www.ruby-forum.com/t/how-to-get-rake-task-to-delete-dependents/175412>\
**Category:** Rails\
**Created:** [September 13, 2009, 6:03am UTC](https://www.ruby-forum.com/t/how-to-get-rake-task-to-delete-dependents/175412 "2009-09-13T06:03:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [September 13, 2009, 6:03am UTC](https://www.ruby-forum.com/t/how-to-get-rake-task-to-delete-dependents/175412/1 "2009-09-13T06:03:37Z")

</div>

is there a way i can get a rake task to destroy the dependents declared  
in the model when an item is destroyed?

here is my rake task code:

task(:deleteFakes =\> :environment) do  
@items = Item.find(:all, :conditions =\> ‘items.fake = true’, :order  
=\> ‘name’)

@items.each do |item|  
item.destroy  
puts item.name + " destroyed!"  
end  
end

and part of the item model:

has\_many :compounds, :dependent =\> :destroy

if i delete the items through the web interface the item and it’s  
associated compounds are removed. is there a way i can get this to work  
from the rake task?

thanks.  
scott

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [September 13, 2009, 6:09am UTC](https://www.ruby-forum.com/t/how-to-get-rake-task-to-delete-dependents/175412/2 "2009-09-13T06:09:08Z")

</div>

Scott K. wrote:

> is there a way i can get a rake task to destroy the dependents declared  
> in the model when an item is destroyed?
> 
> here is my rake task code:
> 
> task(:deleteFakes =\> :environment) do  
> @items = Item.find(:all, :conditions =\> ‘items.fake = true’, :order  
> =\> ‘name’)
> 
> @items.each do |item|  
> item.destroy  
> puts item.name + " destroyed!"  
> end  
> end
> 
> and part of the item model:
> 
> has\_many :compounds, :dependent =\> :destroy
> 
> if i delete the items through the web interface the item and it’s  
> associated compounds are removed. is there a way i can get this to work  
> from the rake task?
> 
> thanks.  
> scott

sorry, i figured it out. it actually is working as expected, i had some  
relations to the item that were not directly linked to the dependent  
clause.
