# How to ensure deletes cascade to associated models?

**URL:** <https://www.ruby-forum.com/t/how-to-ensure-deletes-cascade-to-associated-models/50324>\
**Category:** Rails\
**Created:** [December 15, 2005, 8:01am UTC](https://www.ruby-forum.com/t/how-to-ensure-deletes-cascade-to-associated-models/50324 "2005-12-15T08:01:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![doncarlosx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@doncarlosx](https://www.ruby-forum.com/u/doncarlosx)\
**Post date:** [December 15, 2005, 8:01am UTC](https://www.ruby-forum.com/t/how-to-ensure-deletes-cascade-to-associated-models/50324/1 "2005-12-15T08:01:37Z")

</div>

I have the following models:

class Resume  
belongs\_to :resume\_assignments  
end

class User  
belongs\_to :resume\_assignments  
end

class ResumeAssignment  
has\_one :resume  
has\_one :user  
end

When I delete a resume, I want to make sure that its associated  
ResumeAssignment gets deleted also. Calling destroy\_all to delete  
some resumes doesn’t seem to do this. Is there an option that will  
make it do this?

Thanks,  
Carl

---

<div class="post-metadata">

**Author:** ![doncarlosx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@doncarlosx](https://www.ruby-forum.com/u/doncarlosx)\
**Post date:** [December 15, 2005, 8:22am UTC](https://www.ruby-forum.com/t/how-to-ensure-deletes-cascade-to-associated-models/50324/2 "2005-12-15T08:22:38Z")

</div>

:dependent =\> true

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

---

<div class="post-metadata">

**Author:** ![doncarlosx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@doncarlosx](https://www.ruby-forum.com/u/doncarlosx)\
**Post date:** [December 15, 2005, 8:19pm UTC](https://www.ruby-forum.com/t/how-to-ensure-deletes-cascade-to-associated-models/50324/3 "2005-12-15T20:19:29Z")

</div>

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

> Thanks, but I’m looking for something slightly different. I don’t  
> want resumes or users to be deleted when I delete a ResumeAssignment.  
> I want ResumeAssignments to be deleted when I delete a resume or a  
> user. The ResumeAssignment model used to be a habtm table, but I  
> found myself wanting to store more information in it and found that it  
> would be more useful as a model. Is there a better way of setting up  
> the models to accomplish what I’m trying to do?

Two options:

1. Put in an after\_destroy callback in Resume and User that  
destroys the ResumeAssignment

2. Have Resume has\_one :resume\_assignment and User has\_one  
:resume\_assignment and ResumeAssignment belongs\_to the others. That’s  
probably your best bet.

So,  
class Resume \< AR; has\_one :resume\_assignment :dependent =\> true; end  
class User \< AR; has\_one :resume\_assignment :dependent =\> true; end  
class ResumeAssignment; belongs\_to :resume; belongs\_to :user; end

---

<div class="post-metadata">

**Author:** ![doncarlosx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@doncarlosx](https://www.ruby-forum.com/u/doncarlosx)\
**Post date:** [December 15, 2005, 8:22pm UTC](https://www.ruby-forum.com/t/how-to-ensure-deletes-cascade-to-associated-models/50324/4 "2005-12-15T20:22:31Z")

</div>

On 15-dec-2005, at 19:24, Carl Y. wrote:

> > [http://api.rubyonrails.org/classes/ActiveRecord/Associations/](http://api.rubyonrails.org/classes/ActiveRecord/Associations/)
> > 
> > > belongs\_to :resume\_assignments  
> > > make it do this?  
> > > Try join models in the trunk (if you are still in development and  
> > > don’t mind running edge).

---

<div class="post-metadata">

**Author:** ![doncarlosx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@doncarlosx](https://www.ruby-forum.com/u/doncarlosx)\
**Post date:** [December 15, 2005, 7:25pm UTC](https://www.ruby-forum.com/t/how-to-ensure-deletes-cascade-to-associated-models/50324/5 "2005-12-15T19:25:20Z")

</div>

Thanks, but I’m looking for something slightly different. I don’t  
want resumes or users to be deleted when I delete a ResumeAssignment.  
I want ResumeAssignments to be deleted when I delete a resume or a  
user. The ResumeAssignment model used to be a habtm table, but I  
found myself wanting to store more information in it and found that it  
would be more useful as a model. Is there a better way of setting up  
the models to accomplish what I’m trying to do?

---

<div class="post-metadata">

**Author:** ![doncarlosx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@doncarlosx](https://www.ruby-forum.com/u/doncarlosx)\
**Post date:** [December 16, 2005, 12:10am UTC](https://www.ruby-forum.com/t/how-to-ensure-deletes-cascade-to-associated-models/50324/6 "2005-12-16T00:10:40Z")

</div>

On 12/15/05, Joe Van D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Two options:
> 
> 1. Put in an after\_destroy callback in Resume and User that  
> destroys the ResumeAssignment
> 
> 2. Have Resume has\_one :resume\_assignment and User has\_one  
> :resume\_assignment and ResumeAssignment belongs\_to the others. That’s  
> probably your best bet.

Unfortunately only option one will work for me, since a single resume  
can be assigned to multiple users and vice versa.

Thanks for the help.

Carl

---

<div class="post-metadata">

**Author:** ![doncarlosx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@doncarlosx](https://www.ruby-forum.com/u/doncarlosx)\
**Post date:** [December 16, 2005, 12:35am UTC](https://www.ruby-forum.com/t/how-to-ensure-deletes-cascade-to-associated-models/50324/7 "2005-12-16T00:35:04Z")

</div>

By the way, this idea worked. Thanks Joe

---

<div class="post-metadata">

**Author:** ![doncarlosx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@doncarlosx](https://www.ruby-forum.com/u/doncarlosx)\
**Post date:** [December 16, 2005, 12:13am UTC](https://www.ruby-forum.com/t/how-to-ensure-deletes-cascade-to-associated-models/50324/8 "2005-12-16T00:13:46Z")

</div>

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

> > 1. Have Resume has\_one :resume\_assignment and User has\_one  
> > :resume\_assignment and ResumeAssignment belongs\_to the others. That’s  
> > probably your best bet.
> 
> Unfortunately only option one will work for me, since a single resume  
> can be assigned to multiple users and vice versa.

Or I guess I could make the resume has\_many :resume\_assignments and  
the user has\_many :resume\_assignments and have the ResumeAssignment  
belong\_to the others. That might work.

Carl

---

<div class="post-metadata">

**Author:** ![doncarlosx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@doncarlosx](https://www.ruby-forum.com/u/doncarlosx)\
**Post date:** [December 16, 2005, 6:36am UTC](https://www.ruby-forum.com/t/how-to-ensure-deletes-cascade-to-associated-models/50324/9 "2005-12-16T06:36:23Z")

</div>

So the join keys are in the resume\_assignments table? That’s the way  
it should be, afaik.
