# Has\_one behavior - AWDWR page 332

**URL:** <https://www.ruby-forum.com/t/has-one-behavior-awdwr-page-332/102989>\
**Category:** Rails\
**Created:** [January 22, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/has-one-behavior-awdwr-page-332/102989 "2007-01-22T18:04:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pearl8](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/439d5e/32.png) [@pearl8](https://www.ruby-forum.com/u/pearl8)\
**Post date:** [January 22, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/has-one-behavior-awdwr-page-332/102989/1 "2007-01-22T18:04:23Z")

</div>

Hi,

On AWDWR page 332 there is this example

class Order \< ActiveRecord::Base  
has\_one :invoice  
end

order = Order.new(… attributes …)  
invoice = Invoice.new(… attributes …)  
order.invoice = invoice

And then the explanation: If there is already an existing child object  
when you assign a new object to a has\_one association, that existing  
object will be updated to remove its foreign key association with the  
parent row(the foreign key will be set to null)

I find that however that if:

1. I change has\_one :invoice :dependent =\> :destroy then the existing  
object is actually DELETED rather than just FK being set to null

2. However for the same has\_one :invoice :dependent =\> :destroy if I  
use build\_invoice instead of assignment  
i.e. order.build\_invoice instead of order.invoice = invoice  
then the Existing object’s FK is NULLED out and NOT deleted.

I’m just trying to figure if:

a) Both observations above are correct  
b) Why the difference in behavior - i.e. assignment DELETEs the  
existing record, whereas build\_association NULLs the FK

---

<div class="post-metadata">

**Author:** ![pearl8](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/439d5e/32.png) [@pearl8](https://www.ruby-forum.com/u/pearl8)\
**Post date:** [January 23, 2007, 12:02am UTC](https://www.ruby-forum.com/t/has-one-behavior-awdwr-page-332/102989/2 "2007-01-23T00:02:10Z")

</div>

1. :dependend =\> :destroy is supposed to delete it. If you don’t have  
that defined, it’ll sett the association to null

On Jan 22, 6:04 pm, Karthik N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![pearl8](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/439d5e/32.png) [@pearl8](https://www.ruby-forum.com/u/pearl8)\
**Post date:** [January 23, 2007, 3:17am UTC](https://www.ruby-forum.com/t/has-one-behavior-awdwr-page-332/102989/3 "2007-01-23T03:17:12Z")

</div>

> 1. :dependend =\> :destroy is supposed to delete it. If you don’t have  
> that defined, it’ll sett the association to null

Hi, you’re right however :dependent =\> :destroy as per the  
documentation, is supposed to work when the parent is deleted. however,  
in the examples above, it seems to take effect even for “normal”  
association cases. And in that too, there is a discrepancy,  
build\_association NULLs the FK, but assignment DELETEs the child record!

puzzling …
