# Associated Models and :dependent =\> :destroy

**URL:** <https://www.ruby-forum.com/t/associated-models-and-dependent-destroy/151026>\
**Category:** Rails\
**Created:** [October 16, 2008, 1:58am UTC](https://www.ruby-forum.com/t/associated-models-and-dependent-destroy/151026 "2008-10-16T01:58:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aviir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecb155/32.png) [@aviir](https://www.ruby-forum.com/u/aviir)\
**Post date:** [October 16, 2008, 1:58am UTC](https://www.ruby-forum.com/t/associated-models-and-dependent-destroy/151026/1 "2008-10-16T01:58:36Z")

</div>

Hello,

I’m a rails beginner and I would appreciate any help on this problem.

I have two models, Workbook and WorkbookContent. Workbook holds  
uploaded Excel workbooks (through attachment\_fu). I want to parse the  
data into fields in WorkbookContent.

My Workbook model uses  
`has_one :workbook_content, :dependent => :destroy `

and my WorkbookContent model uses  
`belongs_to :workbook_content, :dependent => :destroy `

My understanding is that, if Workbook includes the dependent =\> destroy  
property, all its children will be destroyed upon its destruction.  
However, when I look in the MySQL database, I can see that the child is  
not being destroyed. Is this something I am doing wrong with my  
migration? My migration for workbook\_\_contents currently contains the  
code:  
`t.references :workbook`  
which I found while scouring the internet. Before, I had no explicit  
mention of workbook and did not explicitly create a workbook\_id column.

I don’t understand why I can’t get the child to be destroyed. Any help  
would be greatly appreciated!

Also, if anyone can guide me to some good reference materials on  
associated models, I would be very grateful.

Thanks!

---

<div class="post-metadata">

**Author:** ![aviir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecb155/32.png) [@aviir](https://www.ruby-forum.com/u/aviir)\
**Post date:** [October 16, 2008, 2:57am UTC](https://www.ruby-forum.com/t/associated-models-and-dependent-destroy/151026/2 "2008-10-16T02:57:54Z")

</div>

On Oct 16, 7:58 am, Avi Ir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and my WorkbookContent model uses  
> `belongs_to :workbook_content, :dependent => :destroy `

Shouldn’t this be:  
belongs\_to :workbook, :dependent =\> :destroy

---

<div class="post-metadata">

**Author:** ![aviir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecb155/32.png) [@aviir](https://www.ruby-forum.com/u/aviir)\
**Post date:** [October 16, 2008, 6:14pm UTC](https://www.ruby-forum.com/t/associated-models-and-dependent-destroy/151026/3 "2008-10-16T18:14:33Z")

</div>

Yes, sorry that was just a typo from copy/paste.

Is there anything I need to do with the migration to get this to work,  
or is it only the :dependent =\> :destroy command?

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![aviir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecb155/32.png) [@aviir](https://www.ruby-forum.com/u/aviir)\
**Post date:** [October 16, 2008, 7:49pm UTC](https://www.ruby-forum.com/t/associated-models-and-dependent-destroy/151026/4 "2008-10-16T19:49:19Z")

</div>

Can’t say as I’ve used the t.references command yet, but a look at your  
table definition for workbook\_contents should tell you whether you have  
a workbook\_id field in there.

What I do find curious is that you have the :dependent =\> :destroy in  
both models, workbook and workbook\_content.

If the user only ever deletes the workbook, then that model is the one  
that needs the dependent clause. I’m not sure the effects of having the  
clause on both models, and I’ve never had to code the dependent -  
destroy from the belongs\_to side of the association.

From the Rails Framework Docs, it does note that dependent can be used  
as an option, but it does caution that:

:dependent - If set to :destroy, the associated object is destroyed when  
this object is. If set to :delete, the associated object is deleted  
without calling its destroy method. This option should not be specified  
when belongs\_to is used in conjunction with a has\_many relationship on  
another class because of the potential to leave orphaned records behind.
