# Deleting all related tuples in all relevant tables

**URL:** <https://www.ruby-forum.com/t/deleting-all-related-tuples-in-all-relevant-tables/81419>\
**Category:** Rails\
**Created:** [January 9, 2007, 12:16am UTC](https://www.ruby-forum.com/t/deleting-all-related-tuples-in-all-relevant-tables/81419 "2007-01-09T00:16:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [January 9, 2007, 12:16am UTC](https://www.ruby-forum.com/t/deleting-all-related-tuples-in-all-relevant-tables/81419/1 "2007-01-09T00:16:41Z")

</div>

So I have three tables, table\_a, table\_c, and table\_u.

both table\_c and table\_u reference table\_a

so basically these three tables are related

If I delete an entry in table\_a, I want all entries in table\_c and  
table\_u that reference table\_a deleted as well.

Is there a way to do this? What does the code look like to do something  
like this?

Thanks!

Gilles

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [January 9, 2007, 12:23am UTC](https://www.ruby-forum.com/t/deleting-all-related-tuples-in-all-relevant-tables/81419/2 "2007-01-09T00:23:01Z")

</div>

I think you just want something like:

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

in your model.

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [January 9, 2007, 12:54am UTC](https://www.ruby-forum.com/t/deleting-all-related-tuples-in-all-relevant-tables/81419/3 "2007-01-09T00:54:03Z")

</div>

I am assuming that you also have the three classes that correspond to  
the tables set up with a has\_many relationship…

So,

def Table\_a \< ActiveRecord::Base  
has\_many :table\_as, :dependent =\> :destroy  
has\_many :table\_us, dependent =\> :destroy  
end

def Table\_b \< ActiveRecord::Base  
belongs\_to :table\_a  
end

def Table\_u \< ActiveRecord::Base  
belongs\_to :table\_a  
end

The :dependent =\> :destroy is all you need in order to be able to do…

Table\_a.find(:first).destroy

…and have all Table\_u and Table\_c entries that reference the first  
entry in Table\_a destroyed.

Have a look at the options here:

[http://api.rubyonrails.org/classes/ActiveRecord/Associations/](http://api.rubyonrails.org/classes/ActiveRecord/Associations/)  
ClassMethods.html#M000530

-christos
