# Problem with saving associated object

**URL:** <https://www.ruby-forum.com/t/problem-with-saving-associated-object/129331>\
**Category:** Ruby\
**Created:** [February 16, 2008, 11:00pm UTC](https://www.ruby-forum.com/t/problem-with-saving-associated-object/129331 "2008-02-16T23:00:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jakub\_KuSSSSma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@Jakub\_KuSSSSma](https://www.ruby-forum.com/u/Jakub_KuSSSSma)\
**Post date:** [February 16, 2008, 11:00pm UTC](https://www.ruby-forum.com/t/problem-with-saving-associated-object/129331/1 "2008-02-16T23:00:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jakub\_KuSSSSma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@Jakub\_KuSSSSma](https://www.ruby-forum.com/u/Jakub_KuSSSSma)\
**Post date:** [February 17, 2008, 3:19pm UTC](https://www.ruby-forum.com/t/problem-with-saving-associated-object/129331/2 "2008-02-17T15:19:29Z")

</div>

On Feb 16, 2008 4:59 PM, Jakub Ku¼ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://talklikeaduck.denhaven2.com/)
