Problem with saving associated object

Hi!
I’ve following problem:

class City < ActiveRecord::Base
has_one :marker
end

class Marker < ActiveRecord::Base
belongs_to :city
end

city = City.find(1)
=> #<City id: 1, name: “Warsaw”, created_at: “2008-02-16 22:25:43”,
updated_at: “2008-02-16 22:26:53”>

city.marker.latitude = 60.0
=> 60.0

city.save
=> true

city.marker.latitude
=> 60.0

city.reload
=> #<City id: 1, name: “Warsaw”, created_at: “2008-02-16 22:25:43”,
updated_at: “2008-02-16 22:28:17”>

city.marker.latitude
=> 50.0

AFAIR it worked in Rails 1.2. How to get behavior like above in Rails
2.0? Any advice would be appreciated :-).

TIA - Jakub Kuźma.

On Feb 16, 2008 4:59 PM, Jakub Ku¼ma [email protected] wrote:

=> #<City id: 1, name: “Warsaw”, created_at: “2008-02-16 22:25:43”,
updated_at: “2008-02-16 22:28:17”>

city.marker.latitude
=> 50.0

AFAIR it worked in Rails 1.2. How to get behavior like above in Rails
2.0? Any advice would be appreciated :-).

You’ll probably get a better response to this on the Rails forum, but.

I’m pretty sure that you’re mistaken about how this worked on 1.2.
Looking at both AR 1.2 and 2.0 code, it looks like the only time
saving a record with a has_one/many relation saves the associated
record is when:

  1. Either the record with the has_x or the associated record is new,
    OR,
  2. if the id in the associated record doesn’t match the id of the
    has_x record

So simply changing an attribute of the associated record wouldn’t
cause the associated record to be updated.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/