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.
Paulo A. (Guest)
on 2008-12-25 03: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 A.
Frederick C. (Guest)
on 2008-12-25 03:12
(Received via mailing list)
On 25 Dec 2008, at 01:08, Paulo A. 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.