Associated Models and :dependent => :destroy

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 <code>has_one :workbook_content, :dependent => :destroy </code>

and my WorkbookContent model uses <code>belongs_to :workbook_content, :dependent => :destroy </code>

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: <code>t.references :workbook</code> 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!

Shouldn't this be: belongs_to :workbook, :dependent => :destroy

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.

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.