# Belongs\_to nonintuitive results with assignment?

**URL:** <https://www.ruby-forum.com/t/belongs-to-nonintuitive-results-with-assignment/74492>\
**Category:** Rails\
**Created:** [November 7, 2006, 10:45am UTC](https://www.ruby-forum.com/t/belongs-to-nonintuitive-results-with-assignment/74492 "2006-11-07T10:45:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![brian.estlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7bcc69/32.png) [@brian.estlin](https://www.ruby-forum.com/u/brian.estlin)\
**Post date:** [November 7, 2006, 10:45am UTC](https://www.ruby-forum.com/t/belongs-to-nonintuitive-results-with-assignment/74492/1 "2006-11-07T10:45:55Z")

</div>

class Page \< ActiveRecord::Base  
has\_many :lists  
end

class List \< ActiveRecord::Base  
belongs\_to :page  
end

######### controller code, inside action:  
list = List.find(params[:id])  
old\_page = list.page  
list.page = new\_page  
# what is the value of old\_page now?

old\_page now is the same as new\_page! what the heck?!?

The only thing i can think of is that old\_page is getting assigned some  
kind of proxy object, rather than the Page object itself, and  
“list.page = new\_page” changes only the actual Page object pointed to  
by the (same) proxy object. Am I on the right track?

The workaround is easy, of course. But would it really be that hard to  
maintain normal object referencing behavior?

Not sure if I’m asking a question or just ranting. But if other people  
don’t see this behavior, or if there’s an elegant explanation of why it  
has to be this way, then I’d like to know.

-Brian

---

<div class="post-metadata">

**Author:** ![brian.estlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7bcc69/32.png) [@brian.estlin](https://www.ruby-forum.com/u/brian.estlin)\
**Post date:** [November 7, 2006, 10:47am UTC](https://www.ruby-forum.com/t/belongs-to-nonintuitive-results-with-assignment/74492/2 "2006-11-07T10:47:30Z")

</div>

Hi –

On Mon, 30 Oct 2006, brian.estlin wrote:

> list = List.find(params[:id])
> 
> The workaround is easy, of course. But would it really be that hard to  
> maintain normal object referencing behavior?
> 
> Not sure if I’m asking a question or just ranting. But if other people  
> don’t see this behavior, or if there’s an elegant explanation of why it  
> has to be this way, then I’d like to know.

I do see it, though I’d never noticed it before. I have no idea what  
the rationale for it is.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![brian.estlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7bcc69/32.png) [@brian.estlin](https://www.ruby-forum.com/u/brian.estlin)\
**Post date:** [November 7, 2006, 10:47am UTC](https://www.ruby-forum.com/t/belongs-to-nonintuitive-results-with-assignment/74492/3 "2006-11-07T10:47:31Z")

</div>

I got bit by this too, but on reflection (no pun intended 😉 it is  
correct, I was actually doing it  
the other way around and assigning the has\_many, but it is the same  
issue.

Just think of it as pointer assignment if you come from c/c++.

One work around in your case is to do this instead…

old\_page\_id= list.page.id

I did this as it was an array…

old\_lists= page.lists.clone

that way I got a clone of the array of lists, not a pointer to the lists  
has\_many proxy. (or  
whatever it is).

brian.estlin wrote:

> ```
> old_page = list.page
> 
> ```
> 
> The workaround is easy, of course. But would it really be that hard to  
> maintain normal object referencing behavior?
> 
> Not sure if I’m asking a question or just ranting. But if other people  
> don’t see this behavior, or if there’s an elegant explanation of why it  
> has to be this way, then I’d like to know.
> 
> -Brian

–  
Jim M., [http://blog.wolfman.com](http://blog.wolfman.com)

---

<div class="post-metadata">

**Author:** ![brian.estlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7bcc69/32.png) [@brian.estlin](https://www.ruby-forum.com/u/brian.estlin)\
**Post date:** [November 7, 2006, 10:48am UTC](https://www.ruby-forum.com/t/belongs-to-nonintuitive-results-with-assignment/74492/4 "2006-11-07T10:48:22Z")

</div>

removed\_email\_address@domain.invalid wrote:

> old\_page\_id = list.page.id  
> This makes for the (to me) strange situation where:
> 
> a = list.page  
> b = Page.find(a.id)
> 
> lead to two different behaviors in the face of changing list.page.  
> I’m not seeing what the purpose or advantage of this is.

Agreed it threw me, but I gave up worrying about the why’s a long time  
ago, I’m just thankful I got  
around the problem without spending hours on it 😉 As a sage once said  
“I am’s What I am’s”, or in  
this case “it is as it is”.

–  
Jim M., [http://blog.wolfman.com](http://blog.wolfman.com)

---

<div class="post-metadata">

**Author:** ![brian.estlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7bcc69/32.png) [@brian.estlin](https://www.ruby-forum.com/u/brian.estlin)\
**Post date:** [November 7, 2006, 10:49am UTC](https://www.ruby-forum.com/t/belongs-to-nonintuitive-results-with-assignment/74492/5 "2006-11-07T10:49:22Z")

</div>

Well, I’m not going to spend too much time worrying about it either.  
But here’s the thing that IMO makes it a bad design. If I show you the  
following code:

old\_page = list.page  
list.page = new\_page

# what is the value of old\_page now?

your answer (as to what old\_page is) will be _different_ depending on  
whether “list.page” represents an ActiveRecord association or not.  
I.e., you can’t tell, just by looking at the above code. You have to  
go track down what “list” is and look in it’s class definition to see  
whether “page” is an association.

Of course, in a small single-developer application, which mine is, it’s  
not really an issue. But if you’re handing a 50,000-line codebase to  
another developer, it’s kind of unfortunate. So IMO it’s a code  
readability/maintainability turkey.

In C pointer assignment, you at least can make a good guess from all  
the little \*'s what’s actually going on. 😉

-Brian

---

<div class="post-metadata">

**Author:** ![brian.estlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7bcc69/32.png) [@brian.estlin](https://www.ruby-forum.com/u/brian.estlin)\
**Post date:** [November 7, 2006, 10:47am UTC](https://www.ruby-forum.com/t/belongs-to-nonintuitive-results-with-assignment/74492/6 "2006-11-07T10:47:59Z")

</div>

Hi –

On Mon, 30 Oct 2006, Jim M. wrote:

> > ```
> > list = List.find(params[:id])
> > 
> > ```
> 
> Just think of it as pointer assignment if you come from c/c++.

I come from Ruby 🙂 It seems very odd to me.

> One work around in your case is to do this instead…
> 
> old\_page\_id= list.page.id

Hey, enjoy the syntactic sugar:

old\_page\_id = list.page.id

🙂

> I did this as it was an array…
> 
> old\_lists= page.lists.clone
> 
> that way I got a clone of the array of lists, not a pointer to the  
> lists has\_many proxy. (or whatever it is).

This makes for the (to me) strange situation where:

a = list.page  
b = Page.find(a.id)

lead to two different behaviors in the face of changing list.page.  
I’m not seeing what the purpose or advantage of this is.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![brian.estlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7bcc69/32.png) [@brian.estlin](https://www.ruby-forum.com/u/brian.estlin)\
**Post date:** [November 7, 2006, 10:52am UTC](https://www.ruby-forum.com/t/belongs-to-nonintuitive-results-with-assignment/74492/7 "2006-11-07T10:52:12Z")

</div>

Hi –

On Tue, 31 Oct 2006, Jim M. wrote:

> > your answer (as to what old\_page is) will be _different_ depending on  
> > In C pointer assignment, you at least can make a good guess from all  
> > the little \*'s what’s actually going on. 😉
> > 
> > -Brian
> 
> I fully agree, it is non-intuitive and very confusing, I wonder what it would take to fix it,  
> without breaking a bunch of other stuff?

I’d argue that if this were gotten rid of, any changes that had to be  
made to things that depended on it would be by way of _fixing_ those  
things 🙂

Disclaimer: I’m still open to hearing a rationale for it from the  
team.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![brian.estlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7bcc69/32.png) [@brian.estlin](https://www.ruby-forum.com/u/brian.estlin)\
**Post date:** [November 7, 2006, 10:49am UTC](https://www.ruby-forum.com/t/belongs-to-nonintuitive-results-with-assignment/74492/8 "2006-11-07T10:49:55Z")

</div>

brian.estlin wrote:

> I.e., you can’t tell, just by looking at the above code. You have to
> 
> -Brian

I fully agree, it is non-intuitive and very confusing, I wonder what it  
would take to fix it,  
without breaking a bunch of other stuff?

–  
Jim M., [http://blog.wolfman.com](http://blog.wolfman.com)
