# Is there an AR.delete CASCADE option?

**URL:** <https://www.ruby-forum.com/t/is-there-an-ar-delete-cascade-option/47625>\
**Category:** Rails\
**Created:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/is-there-an-ar-delete-cascade-option/47625 "2005-11-12T12:49:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![carl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@carl](https://www.ruby-forum.com/u/carl)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/is-there-an-ar-delete-cascade-option/47625/1 "2005-11-12T12:49:44Z")

</div>

I’m trying to delete an AR model with an habtm association with  
another table that is enforced with foreign key rule. The query I’m  
running is:

```
  Resume.delete_all(["found_on <= ?", date])

```

But the delete is failing because the rows in the other table haven’t  
been deleted first. Is there an easy way in AR to cause this delete  
to cascade without having to select out the IDs of all the rows that  
would be affected by this delete, delete them first and then delete  
these rows?

Thanks,

Carl

---

<div class="post-metadata">

**Author:** ![carl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@carl](https://www.ruby-forum.com/u/carl)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/is-there-an-ar-delete-cascade-option/47625/2 "2005-11-12T12:49:45Z")

</div>

On 11/9/05, Carl Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> would be affected by this delete, delete them first and then delete  
> these rows?
> 
> Thanks,

Assuming your database supports it, I would that if you are using a  
foreign  
key in the database and want it to cascade, then set it that way in the  
database, otherwise don’t use a foreign key at all. In other words all  
or  
nothing is probably the best approach.

With rails I’ve started to avoid using foreign keys unless they are  
really  
needed, especially if I want the app to support all the rails supported  
databases. Look at the callbacks section in the ActiveRecord  
documentation.  
You can define a before\_delete callback to do the deletions (even though  
that callback isn’t documented it does exist).

after\_find is another nice callback and it’s not listed as a callback,  
although it’s referred to briefly.

Chris

---

<div class="post-metadata">

**Author:** ![carl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@carl](https://www.ruby-forum.com/u/carl)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/is-there-an-ar-delete-cascade-option/47625/3 "2005-11-12T12:49:45Z")

</div>

Carl Y. wrote:

> these rows?
> 
> Thanks,
> 
> Carl

See the :dependent and :exclusively\_dependent options when you set up  
the  
relationships.

[http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html)

Regards,  
Blair

---

<div class="post-metadata">

**Author:** ![carl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@carl](https://www.ruby-forum.com/u/carl)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/is-there-an-ar-delete-cascade-option/47625/4 "2005-11-12T12:49:45Z")

</div>

Thanks. In this case, things got deleted automatically when I used  
destroy\_all instead of delete\_all.

Carl

---

<div class="post-metadata">

**Author:** ![carl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@carl](https://www.ruby-forum.com/u/carl)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/is-there-an-ar-delete-cascade-option/47625/5 "2005-11-12T12:49:46Z")

</div>

One small wrinkle I noticed with using that is that with children that  
utilize file\_column the related files in the file system do not get  
purged when the db records do … this, of course, makes sense but meant  
I had to make use of destroy instead…

Tim
