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)
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?