# Remove instance of model and its associations

**URL:** <https://www.ruby-forum.com/t/remove-instance-of-model-and-its-associations/142282>\
**Category:** Rails\
**Created:** [July 5, 2008, 11:22pm UTC](https://www.ruby-forum.com/t/remove-instance-of-model-and-its-associations/142282 "2008-07-05T23:22:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![THEBIGO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@THEBIGO](https://www.ruby-forum.com/u/THEBIGO)\
**Post date:** [July 5, 2008, 11:22pm UTC](https://www.ruby-forum.com/t/remove-instance-of-model-and-its-associations/142282/1 "2008-07-05T23:22:43Z")

</div>

I’m a newb with a quick question.

I have a model  
class Call  
has\_many :visits  
and  
class Visit  
belongs\_to :call

If I delete a call will it automatically delete the associated  
visits??  
Any help will be greatly appreciated

---

<div class="post-metadata">

**Author:** ![THEBIGO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@THEBIGO](https://www.ruby-forum.com/u/THEBIGO)\
**Post date:** [July 6, 2008, 12:19am UTC](https://www.ruby-forum.com/t/remove-instance-of-model-and-its-associations/142282/2 "2008-07-06T00:19:26Z")

</div>

On Jul 5, 2008, at 5:22 PM, THEBIGO wrote:

> I’m a newb with a quick question.
> 
> I have a model  
> class Call  
> has\_many :visits  
> has\_many :visits, :dependent =\> :destroy

this should do the trick.

Best.  
Mike

---

<div class="post-metadata">

**Author:** ![THEBIGO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@THEBIGO](https://www.ruby-forum.com/u/THEBIGO)\
**Post date:** [July 6, 2008, 12:26am UTC](https://www.ruby-forum.com/t/remove-instance-of-model-and-its-associations/142282/3 "2008-07-06T00:26:27Z")

</div>

On Jul 5, 2008, at 5:22 PM, THEBIGO wrote:

> If I delete a call will it automatically delete the associated  
> visits??

Sorry, I was too quick on the draw with the last email.

The association you have will not delete the visits. The last message  
I posted will destroy associated visits when you destroy the call.

If you don’t want AR callbacks to fire on the associated vists you can  
use has\_many :visits, :dependent =\> :delete\_all

Best.  
Mike

---

<div class="post-metadata">

**Author:** ![THEBIGO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@THEBIGO](https://www.ruby-forum.com/u/THEBIGO)\
**Post date:** [July 6, 2008, 12:38am UTC](https://www.ruby-forum.com/t/remove-instance-of-model-and-its-associations/142282/4 "2008-07-06T00:38:08Z")

</div>

Mike - Thank you for your response so I take it that if I have say 20  
calls and call with call.id = 5 and if  
there are say 11 visits with various visit.id’s but with their call\_id  
= 5 linking them to the above call  
then when I execute the delete action for call with id=5 then it will  
be deleted along with it’s 11 visits  
then I should use the first one you said

has\_many :visits, :dependent =\> :destroy ???

Thanks again

---

<div class="post-metadata">

**Author:** ![THEBIGO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@THEBIGO](https://www.ruby-forum.com/u/THEBIGO)\
**Post date:** [July 6, 2008, 12:45am UTC](https://www.ruby-forum.com/t/remove-instance-of-model-and-its-associations/142282/5 "2008-07-06T00:45:43Z")

</div>

On Jul 5, 2008, at 6:37 PM, THEBIGO wrote:

> Mike - Thank you for your response so I take it that if I have say 20  
> calls and call with call.id = 5 and if  
> there are say 11 visits with various visit.id’s but with their call\_id  
> = 5 linking them to the above call  
> then when I execute the delete action for call with id=5 then it will  
> be deleted along with it’s 11 visits  
> then I should use the first one you said
> 
> has\_many :visits, :dependent =\> :destroy ???

From the API

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

:

dependent - If set to :destroy all the associated objects are  
destroyed alongside this object by calling their destroy method. If  
set to :delete\_all all associated objects are deleted without calling  
their destroy method. If set to :nullify all associated objects’  
foreign keys are set to NULL without calling their save callbacks.  
_Warning:_ This option is ignored when also using the :through option.

Best.  
Mike

---

<div class="post-metadata">

**Author:** ![THEBIGO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@THEBIGO](https://www.ruby-forum.com/u/THEBIGO)\
**Post date:** [July 6, 2008, 1:03am UTC](https://www.ruby-forum.com/t/remove-instance-of-model-and-its-associations/142282/6 "2008-07-06T01:03:50Z")

</div>

Thank you mike so very much - it worked like a charm. One more  
itteration complete  
Owen
