Forum: Ruby on Rails save / update related object

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C310c53b455594a3d1b399692f01d38c?d=identicon&s=25 Paulo Abreu (Guest)
on 2008-12-25 02:08
(Received via mailing list)
Imagine that I have the following objects:

class Person < ActiveRecord::Base
  has_one :nose
end

class Nose < ActiveRecord::Base
end

p = Person.new
p.name = "Mary"
p.nose = Nose.new
p.save

p.nose.colour = "white"
p.save

Why in the last p.save, p.nose.colour is not persisted in database? Is
there any way of doing this in an automatic way?

Regards,

Paulo Abreu
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-25 02:12
(Received via mailing list)
On 25 Dec 2008, at 01:08, Paulo Abreu wrote:

> p = Person.new
> p.name = "Mary"
> p.nose = Nose.new
> p.save
>
> p.nose.colour = "white"
> p.save
>
> Why in the last p.save, p.nose.colour is not persisted in database? Is
> there any way of doing this in an automatic way?

Because p.save only saves child objects if they are unsaved.

Fred
This topic is locked and can not be replied to.