# Association can't be changed

**URL:** <https://www.ruby-forum.com/t/association-cant-be-changed/128927>\
**Category:** Rails\
**Created:** [February 12, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/association-cant-be-changed/128927 "2008-02-12T19:26:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![threeeyedfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@threeeyedfish](https://www.ruby-forum.com/u/threeeyedfish)\
**Post date:** [February 12, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/association-cant-be-changed/128927/1 "2008-02-12T19:26:18Z")

</div>

Hi, all!  
May you ever feel happy!

I’m getting some very strange behaviour and I’m not sure where my  
mistake lies.

I have a Divination model that belongs\_to :book.

I get the following in script/console:

> > d = Divination.find(7) # =\> #\<Divination id: 7, user\_id: 1, book\_id: 4…  
> > d.book # =\> #\<Book id: 4…  
> > Now if I change the book:
> 
> > d.book = Book.find(5) # =\> #\<Book id: 5…  
> > d.book # =\> #\<Book id: 4

# It’s still the same!

Now I try to change the book\_id:

> > d.book\_id = 6 # =\> 6  
> > d.book\_id # =\> 6  
> > Wow! It changed!
> 
> > d.book # =\> #\<Book id: 4  
> > But alas! The book is the same!

It certainly shouldn’t be this way, should it? Or am I missing something  
very important?

Yours,  
Damian

P.S. It is Rails 2.0.2 on Mac OS X 10.5 Leopard

---

<div class="post-metadata">

**Author:** ![threeeyedfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@threeeyedfish](https://www.ruby-forum.com/u/threeeyedfish)\
**Post date:** [February 12, 2008, 7:41pm UTC](https://www.ruby-forum.com/t/association-cant-be-changed/128927/2 "2008-02-12T19:41:40Z")

</div>

> Now if I change the book:
> 
> > > d.book = Book.find(5) # =\> #\<Book id: 5…  
> > > d.book # =\> #\<Book id: 4

should be:  
d.book \<\< Book.find(5)

> Now I try to change the book\_id:
> 
> > > d.book\_id = 6 # =\> 6  
> > > d.book\_id # =\> 6  
> > > Wow! It changed!
> > 
> > > d.book # =\> #\<Book id: 4

not sure, but maybe you would have to save the thing, before the id  
change changes the book?
