# RANT: belongs\_to -\> refers\_to

**URL:** <https://www.ruby-forum.com/t/rant-belongs-to-refers-to/56675>\
**Category:** Rails\
**Created:** [April 2, 2006, 5:16pm UTC](https://www.ruby-forum.com/t/rant-belongs-to-refers-to/56675 "2006-04-02T17:16:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [April 2, 2006, 5:16pm UTC](https://www.ruby-forum.com/t/rant-belongs-to-refers-to/56675/1 "2006-04-02T17:16:13Z")

</div>

Every once in a while, I pipe up and whine that “belongs\_to” should  
really  
be 'refers\_to" [[http://dev.rubyonrails.org/ticket/2130](http://dev.rubyonrails.org/ticket/2130)]. It’s time  
again.

I’ve got a bunch of stuff going onto eBay, so, like any good engineer, I  
don’t just post it manually: I design a Rails-based inventory database  
that  
creates a semantically-correct XHTML/CSS auction posting with a variable  
number of thumbnails (stored with file\_column) and Textile fields.

I start out with

class Item \< ActiveRecord::Base  
:has\_many :pictures  
end class

But I want one of those pictures to be featured as the gallery photo.  
So,  
naturally, I gravitate towards adding

class Item \< ActiveRecord::Base  
:has\_one primary\_picture, :class\_name =\> ‘Picture’  
end

I am encouraged in this futile pursuit by the docs, which I quickly skim  
to  
see things like

:has\_one :last\_comment, :class\_name =\> “Comment”, :order =\>  
“posted\_on”

Of course, in the light of day, it’s clear that this automagically  
creates  
the last\_comment association by picking the most recent comment. But at  
3am, it just reinforced the tendency to use has\_one. Ditto the many  
other  
has\_one examples I found - half of which used the automatic-first-record  
feature, and half of which were probably just making the same mistake I  
was.

I started getting nil objects, and was mystified until I finally tried  
explicitly setting the foreign key:

:has\_one primary\_picture, :class\_name =\> ‘Picture’,  
:foreign\_key =\> ‘primary\_picture\_id’

at which point Rails helpfully told me that there was no  
Picture.primary\_picture\_id field. Then I remembered for the umpteenth  
time  
that has\_one is for the record that does NOT have the foreign key, and  
belongs\_to is for the one that does, and I should not be misled by the  
semantics of the actual has\_one and belongs\_to keywords.

Item :belongs\_to primary\_picture makes no sense. Item :refers\_to  
primary\_picture does. This is way too small for even a plugin - and I  
don’t think that type of syntax change belongs in a plugin, anyway. I  
have  
yet to hear anyone who’s opposed to it… can’t we PLEASE add this alias  
and consider deprecating belongs\_to over time? It’s… just… wrong.

Jay L.

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [April 2, 2006, 6:55pm UTC](https://www.ruby-forum.com/t/rant-belongs-to-refers-to/56675/2 "2006-04-02T18:55:17Z")

</div>

On Sun, Apr 02, 2006 at 11:12:47AM -0400, Jay L. wrote:  
} Every once in a while, I pipe up and whine that “belongs\_to” should  
really  
} be 'refers\_to" [[http://dev.rubyonrails.org/ticket/2130](http://dev.rubyonrails.org/ticket/2130)]. It’s time  
again.  
[…]  
} Item :belongs\_to primary\_picture makes no sense. Item :refers\_to  
} primary\_picture does. This is way too small for even a plugin - and I  
} don’t think that type of syntax change belongs in a plugin, anyway. I  
have  
} yet to hear anyone who’s opposed to it… can’t we PLEASE add this  
alias  
} and consider deprecating belongs\_to over time? It’s… just… wrong.

In some contexts it is wrong. In others it is right, such as has\_many.  
Perhaps this would make your life easier:

class ActiveRecord::Base  
class \<\< self  
alias :refers\_to :belongs\_to  
end  
end

} Jay L.  
–Greg

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [April 2, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/rant-belongs-to-refers-to/56675/3 "2006-04-02T19:04:33Z")

</div>

If you really want this, just place this in your environment.rb

module ActiveRecord::Associations::ClassMethods  
alias :refers\_to :belongs\_to  
end

And there you have your refers\_to method.

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [April 2, 2006, 6:52pm UTC](https://www.ruby-forum.com/t/rant-belongs-to-refers-to/56675/4 "2006-04-02T18:52:15Z")

</div>

I’m opposed to it. I’m not sure if your problem is with grammar or  
with the visual abstraction of what you are trying to do, but the  
semantics are correct. Think of it this way:

Let’s say I have 50 widgets that I want to sell on eBay. Each of  
these widgets will have its own auction posting. However, all the  
widgets look alike, so I have a single picture of the widget. I  
don’t want to have 50 pictures of my widget, so it would be incorrect  
to say that each widget has\_one picture. Instead, each widget  
belongs\_to that picture of a widget.

Now, you are using the term Item, so perhaps you have another model  
to handle postings of the Item on eBay. This would change the  
argument above slightly, but the semantics remain the same. If a  
picture is used on only a single Item, but an item might have  
multiple pictures (which is how I interpret this domain), then the  
foreign key should be on the picture, not on the Item, and then you  
would be correctly using the has\_one and has\_many.

Now, by placing the foreign key on the picture, you can achieve the  
concept of a primary picture by placing a boolean flag on the picture  
tuple akin to IsPrimary. Then set up your item model with a has\_many  
picture and a has\_one primary picture (with the constraint that it  
must have the IsPrimary flag set).

OR… you can have a has\_may :pictures, belongs\_to :primary\_picture

OR… you can use acts\_as\_list on your has\_many and maintain that the  
first item in the list is the primary picture. I kinda like this  
approach.

-Derrick S.

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [April 2, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/rant-belongs-to-refers-to/56675/5 "2006-04-02T19:07:48Z")

</div>

Improve your code

class Picture \< AR:B  
acts\_as\_list  
end

class Item \< ActiveRecord::Base  
has\_one :primary\_picture, :class\_name =\> ‘Picture’, :order =\>  
‘position ASC’  
end

On 4/2/06, Jay L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Item \< ActiveRecord::Base  
> I am encouraged in this futile pursuit by the docs, which I quickly skim to
> 
> semantics of the actual has\_one and belongs\_to keywords.  
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Tobi  
[http://shopify.com](http://shopify.com) - modern e-commerce software  
[http://typo.leetsoft.com](http://typo.leetsoft.com) - Open source weblog engine  
[http://blog.leetsoft.com](http://blog.leetsoft.com) - Technical weblog

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [April 2, 2006, 8:21pm UTC](https://www.ruby-forum.com/t/rant-belongs-to-refers-to/56675/6 "2006-04-02T20:21:38Z")

</div>

On Sun, 2 Apr 2006 12:51:54 -0400, Gregory S. wrote:

> In some contexts it is wrong. In others it is right, such as has\_many.  
> Perhaps this would make your life easier:
> 
> class ActiveRecord::Base  
> class \<\< self  
> alias :refers\_to :belongs\_to  
> end  
> end

Yep, I keep swearing I’m gonna do that. I just think it might help  
other  
programmers as well - I often see belongs\_to confusion here, especially  
among novices.

Jay

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [April 2, 2006, 8:18pm UTC](https://www.ruby-forum.com/t/rant-belongs-to-refers-to/56675/7 "2006-04-02T20:18:31Z")

</div>

On Sun, 2 Apr 2006 12:50:22 -0400, Derrick S. wrote:

> Now, you are using the term Item, so perhaps you have another model  
> to handle postings of the Item on eBay. This would change the  
> argument above slightly, but the semantics remain the same. If a  
> picture is used on only a single Item, but an item might have  
> multiple pictures (which is how I interpret this domain), then the  
> foreign key should be on the picture, not on the Item, and then you  
> would be correctly using the has\_one and has\_many.

Your second guess is right - I’m not actually posting to eBay with this  
model (yet), and the items are one-of-a-kind, so there’s no habtm  
relationship; each picture is associated with one and only one item, but  
each item has\_many pictures.

> Now, by placing the foreign key on the picture, you can achieve the  
> concept of a primary picture by placing a boolean flag on the picture  
> tuple akin to IsPrimary. Then set up your item model with a has\_many  
> picture and a has\_one primary picture (with the constraint that it  
> must have the IsPrimary flag set).

The problem with that is that there’s no way to enforce “one and only  
one  
primary picture” if the boolean’s in the picture.

> OR… you can have a has\_may :pictures, belongs\_to :primary\_picture

Which is what I’m doing now, but semantically, in this context, an item  
doesn’t belong to a picture - it’s the other way around. Don’t you  
think?

> OR… you can use acts\_as\_list on your has\_many and maintain that the  
> first item in the list is the primary picture. I kinda like this  
> approach.

Yep, I hadn’t thought of that, but it makes the most sense, and probably  
would have fallen out once I started thinking about being able to  
re-order  
the pictures! That one’s nice.

Jay

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [April 2, 2006, 8:24pm UTC](https://www.ruby-forum.com/t/rant-belongs-to-refers-to/56675/8 "2006-04-02T20:24:52Z")

</div>

Jay,

Just wanted to say that I pretty much agree with you. I was in  
basically the exact same situation you were the other day, with a  
listing of Items where each Item has\_many :images, and I thought I could  
do “has\_one :main\_image”, like you described. Of course I realized the  
same thing you did and it still feels kinda funny, since semantically,  
my Item doesn’t belong to its main picture, the picture belongs to the  
item.

They even take time in Agile Web Dev to say “we know it seems funny to  
say ‘belongs\_to’, so think of it as ‘refers\_to’”. Not sure I understand  
why it was coded that way in the first place, since so many of Rails  
conventions are based on easily translating coding semantics into  
natural language semantics.

Jeff
