# Deleting ALL info for a user in many tables?

**URL:** <https://www.ruby-forum.com/t/deleting-all-info-for-a-user-in-many-tables/109556>\
**Category:** Rails\
**Created:** [July 16, 2007, 8:40pm UTC](https://www.ruby-forum.com/t/deleting-all-info-for-a-user-in-many-tables/109556 "2007-07-16T20:40:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 16, 2007, 8:40pm UTC](https://www.ruby-forum.com/t/deleting-all-info-for-a-user-in-many-tables/109556/1 "2007-07-16T20:40:02Z")

</div>

I have this model called “User.” In the user model I have many relations  
hips such as:

```
has_many :creditcards
has_many :airlinememberships

```

and in my airlinemembership model I have

belongs\_to :user

Now, what I would like to do is to delete not only the user but also the  
airlinmembership info asssocciated with that user when a delete action  
occurs. Now, I suppose I could do a find\_by\_id( user.id ) to go through  
all the tables and destroy all assocciated info, but I’m thinkin that  
there has to be an easier way in RoR. Plus, there are many many tables  
that a user may have info in which make the code not ugly but certaintly  
long.

Also, I know about the rails framework API: [http://api.rubyonrails.org/](http://api.rubyonrails.org/),  
but it seems like this site fails to really explain all the attributes  
that can be used in a helper method, for example. I was wondering if  
anyone knows of a better site that explains maybe a little better or  
might provide some better examples? Thanks all,

~S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 16, 2007, 8:48pm UTC](https://www.ruby-forum.com/t/deleting-all-info-for-a-user-in-many-tables/109556/2 "2007-07-16T20:48:51Z")

</div>

Shandy N. wrote:

> I have this model called “User.” In the user model I have many relations  
> hips such as:
> 
> ```
> has_many :creditcards
> has_many :airlinememberships
> 
> ```
> 
> and in my airlinemembership model I have
> 
> belongs\_to :user
> 
> Now, what I would like to do is to delete not only the user but also the  
> airlinmembership info asssocciated with that user when a delete action  
> occurs. Now, I suppose I could do a find\_by\_id( user.id ) to go through  
> all the tables and destroy all assocciated info, but I’m thinkin that  
> there has to be an easier way in RoR. Plus, there are many many tables  
> that a user may have info in which make the code not ugly but certaintly  
> long.
> 
> Also, I know about the rails framework API: [http://api.rubyonrails.org/](http://api.rubyonrails.org/),  
> but it seems like this site fails to really explain all the attributes  
> that can be used in a helper method, for example. I was wondering if  
> anyone knows of a better site that explains maybe a little better or  
> might provide some better examples? Thanks all,
> 
> ~S

Couldn’t you just use the foreign key and set the option cascade on  
delete? So that way, any record that has your client\_id would be  
deleted. I hope this helps.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 16, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/deleting-all-info-for-a-user-in-many-tables/109556/3 "2007-07-16T21:14:59Z")

</div>

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

That should do the trick. Look it up for more info.

–Tyler P.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 16, 2007, 10:28pm UTC](https://www.ruby-forum.com/t/deleting-all-info-for-a-user-in-many-tables/109556/4 "2007-07-16T22:28:47Z")

</div>

I did the cascade on delete but the :dependent =\> :destroy is what I was  
looking for. Thanks you guys, either way it works perfectly.

~S
