# Accepts\_nested\_attributes\_for and has\_one

**URL:** <https://www.ruby-forum.com/t/accepts-nested-attributes-for-and-has-one/171759>\
**Category:** Rails\
**Created:** [July 8, 2009, 7:17pm UTC](https://www.ruby-forum.com/t/accepts-nested-attributes-for-and-has-one/171759 "2009-07-08T19:17:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 8, 2009, 7:17pm UTC](https://www.ruby-forum.com/t/accepts-nested-attributes-for-and-has-one/171759/1 "2009-07-08T19:17:38Z")

</div>

Hi all, I’m seeing some unexpected behaviour in a nested model and I’m  
not sure if it’s a bug or if I’m doing something wrong. When  
assigning nested attributes, the associated object gets replaced with  
a new record.

Example code:

class Car \< ActiveRecord::Base  
belongs\_to :driver  
end

class Car \< ActiveRecord::Base  
belongs\_to :driver  
end

Here’s some console output:  
(pastie version: [http://pastie.org/538782](http://pastie.org/538782))

> > d = Driver.create :name =\> “Homer”, :age =\> 40  
> > =\> #\<Driver id: 1, name: “Homer”, age: 40, created\_at: “2009-07-08  
> > 16:54:44”, updated\_at: “2009-07-08 16:54:44”\>  
> > d.create\_car :make =\> “Porshce”, :model =\> “911”  
> > =\> #\<Car id: 1, driver\_id: 1, make: “Porshce”, model: “911”,  
> > created\_at: “2009-07-08 16:55:26”, updated\_at: “2009-07-08 16:55:26”\>  
> > d.new\_record?  
> > =\> false  
> > d.car.new\_record?  
> > =\> false  
> > d.car.id  
> > =\> 1  
> > d.car.attributes  
> > =\> {“model”=\>“911”, “driver\_id”=\>1, “created\_at”=\>Wed Jul 08 16:55:26  
> > UTC 2009, “updated\_at”=\>Wed Jul 08 16:55:26 UTC 2009, “id”=\>1,  
> > “make”=\>“Porshce”}  
> > attrs = {:car\_attributes =\> {:model =\> “Boxster”}}  
> > =\> {:car\_attributes=\>{:model=\>“Boxster”}}  
> > d.attributes = attrs  
> > =\> {:car\_attributes=\>{:model=\>“Boxster”}}  
> > d.car.attributes  
> > =\> {“model”=\>“Boxster”, “driver\_id”=\>1, “created\_at”=\>nil,  
> > “updated\_at”=\>nil, “make”=\>nil}  
> > d.car.id  
> > =\> nil  
> > d.car.new\_record?  
> > =\> true

any ideas?
