# Nested Model Destruction

**URL:** <https://www.ruby-forum.com/t/nested-model-destruction/181918>\
**Category:** Rails\
**Created:** [January 20, 2010, 6:36am UTC](https://www.ruby-forum.com/t/nested-model-destruction/181918 "2010-01-20T06:36:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@tony](https://www.ruby-forum.com/u/tony)\
**Post date:** [January 20, 2010, 6:36am UTC](https://www.ruby-forum.com/t/nested-model-destruction/181918/1 "2010-01-20T06:36:49Z")

</div>

Hey all,

I’m using nested model creation in my forms, but I’ve noticed that  
when I go to try and destroy a nested model, the backing DB row gets  
destroyed but the child object is never set back to nil as I expected  
it to be. This leaves me with an object that doesn’t have a backing  
DB row. The object is, however, frozen and I can call “destroyed?” on  
the object which returns true. Is this expected and defined  
behavior? I couldn’t find this referenced anywhere as it seems that  
the docs indicate the object would be set back to nil.

I’m simply resetting the object to nil if it has been destroyed after  
I save the record, so this isn’t a problem, just not something I  
expected. Maybe the docs should be updated?

Some background info: I’m using rails 2.3.5 running on jruby-1.3.1.  
This is a has\_one relationship and :allow\_destroy is set to true in  
the parent model.

Documentation I was looking at:

> **[ActiveRecord::NestedAttributes::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html)**
>
> Active Record Nested Attributes Nested attributes allow you to save attributes on associated records through the parent.

Thanks!

tony

---

<div class="post-metadata">

**Author:** ![tony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@tony](https://www.ruby-forum.com/u/tony)\
**Post date:** [January 20, 2010, 8:51am UTC](https://www.ruby-forum.com/t/nested-model-destruction/181918/2 "2010-01-20T08:51:59Z")

</div>

Hi,

Maybe this can help (taken from Rails Guides -  
[Active Record Associations — Ruby on Rails Guides](http://guides.rubyonrails.org/association_basics.html#belongs-to-association-reference)):

4.1.2.5 :dependent

If you set the :dependent option to :destroy, then deleting this  
object will call the destroy method on the associated object to delete  
that object. If you set the :dependent option to :delete, then  
deleting this object will delete the associated object without calling  
its destroy method.

Best,  
Martin

---

<div class="post-metadata">

**Author:** ![tony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@tony](https://www.ruby-forum.com/u/tony)\
**Post date:** [January 20, 2010, 7:53pm UTC](https://www.ruby-forum.com/t/nested-model-destruction/181918/3 "2010-01-20T19:53:22Z")

</div>

Thanks for the response.

I’m not actually deleting the parent object, just deleting a nested  
object (the child) from a has\_one relationship. Everything is  
working, just simply behaving differently from the documentation. I  
just wanted to confirm that I am seeing the appropriate behavior. It  
seems the documentation may be out of date.

tony
