# Destroying entities related by foreign key

**URL:** <https://www.ruby-forum.com/t/destroying-entities-related-by-foreign-key/161903>\
**Category:** Rails\
**Created:** [March 3, 2009, 5:19pm UTC](https://www.ruby-forum.com/t/destroying-entities-related-by-foreign-key/161903 "2009-03-03T17:19:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 3, 2009, 5:19pm UTC](https://www.ruby-forum.com/t/destroying-entities-related-by-foreign-key/161903/1 "2009-03-03T17:19:45Z")

</div>

hi all,

I have a table “memberships”. It has a field user\_id, which I want to  
use as a foreign key to the “users” table.

So, as far as I get it, in the membership model I have to write:  
has\_many :users

In the users model, I write:  
belongs\_to :membership

However(in case the above is correct), how do I ensure that if I delete  
a user, the related entities in membership will be also deleted??

Thank you 🙂

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 3, 2009, 5:32pm UTC](https://www.ruby-forum.com/t/destroying-entities-related-by-foreign-key/161903/2 "2009-03-03T17:32:01Z")

</div>

Pesho P. wrote:

> hi all,
> 
> I have a table “memberships”. It has a field user\_id, which I want to  
> use as a foreign key to the “users” table.

In that case you should have :  
“has\_many :memberships” in the User model class  
and “belongs\_to :user” in the Membership model class

If you want all related memberships destroyed when deleting a user, you  
must write this in the User class:  
“has\_many :memberships, :dependent=\>:destroy”

(See

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001789)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

for more info about :destroy option)

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 3, 2009, 5:43pm UTC](https://www.ruby-forum.com/t/destroying-entities-related-by-foreign-key/161903/3 "2009-03-03T17:43:47Z")

</div>

On Tue, Mar 3, 2009 at 5:19 PM, Pesho P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> a user, the related entities in membership will be also deleted??  
> Check the options for “has\_one” and “has\_many” (e.g. on page 329 and  
> page 331 in the Second Edition of “Agile Web D. with Rails”).

Use “destroy” on the parent object (not delete).

The option to the has\_one or has\_many you want is:

:dependent =\> :destroy

or

:dependent =\> :delete\_all

depending on your design. The child row(s) will be destroyed at the time  
the  
parent is destroyed.

HTH,

Peter

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 3, 2009, 5:53pm UTC](https://www.ruby-forum.com/t/destroying-entities-related-by-foreign-key/161903/4 "2009-03-03T17:53:51Z")

</div>

Thank you, guys 🙂
