# When (not) to use belongs\_to

**URL:** <https://www.ruby-forum.com/t/when-not-to-use-belongs-to/86404>\
**Category:** Rails\
**Created:** [March 24, 2006, 12:42am UTC](https://www.ruby-forum.com/t/when-not-to-use-belongs-to/86404 "2006-03-24T00:42:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 24, 2006, 12:42am UTC](https://www.ruby-forum.com/t/when-not-to-use-belongs-to/86404/1 "2006-03-24T00:42:15Z")

</div>

I know you get a lot of questions like this, but I couldn’t find one  
which  
answers exactly what I’m after. I’m reasonably new to rails, and I’m  
having  
a bit of trouble with when to use belongs\_to. On the rails wiki (  
[http://wiki.rubyonrails.org/rails/pages/belongs\_to](http://wiki.rubyonrails.org/rails/pages/belongs_to)), it says “In  
general,  
the Foo model belongs\_to :bar if the foo table has a bar\_id foreign key  
column.” But according to ForumExample (  
[http://wiki.rubyonrails.org/rails/pages/ForumExample](http://wiki.rubyonrails.org/rails/pages/ForumExample)) doesn’t always do  
this. A Message has\_one author but there’s no ‘belongs\_to :message’ in  
the  
Author class. There are examples of when the author uses it, and  
examples of  
when he doesn’t.  
Another thing that confused me was the fact that the DBModel page on the  
wiki says “Note that you’ll never need to label a link belongs\_to â??  
relationships are defined from the source table viewpoint, so the  
belongs\_tois implicit in the relationship.” I wasn’t sure if that’s  
just referring to  
use of this particular gem (which I can’t get to work, but I’ve managed  
without) or generally.  
Any help much appreciated.  
-Nathan

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 24, 2006, 1:23am UTC](https://www.ruby-forum.com/t/when-not-to-use-belongs-to/86404/2 "2006-03-24T01:23:14Z")

</div>

unknown wrote:

> the Foo model belongs\_to :bar if the foo table has a bar\_id foreign key  
> column." But according to ForumExample (  
> [http://wiki.rubyonrails.org/rails/pages/ForumExample](http://wiki.rubyonrails.org/rails/pages/ForumExample)) doesn’t always do  
> this. A Message has\_one author but there’s no ‘belongs\_to :message’ in  
> the  
> Author class. There are examples of when the author uses it, and  
> examples of  
> when he doesn’t.

Yes, a Message has\_one author because when displaying the message you’ll  
need to put some informations about his author (name, email).  
But, maybe you’ll never need to list every message written by an author,  
in that case you don’t need to use an association between Author and  
Message (even if it seems sensible and useful, but that’s just for the  
example).

BTW, the relation between Author and Message would not be a belongs\_to  
but has\_many.

So, if one sided you’ll have  
Message has\_one Author  
And if you want it two sided you’ll have also  
Author has\_many Message

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 24, 2006, 2:01am UTC](https://www.ruby-forum.com/t/when-not-to-use-belongs-to/86404/3 "2006-03-24T02:01:27Z")

</div>

removed\_email\_address@domain.invalid wrote:

> On the rails  
> wiki ( [http://wiki.rubyonrails.org/rails/pages/belongs\_to](http://wiki.rubyonrails.org/rails/pages/belongs_to)), it says “In  
> general, the Foo model belongs\_to :bar if the foo table has a bar\_id  
> foreign key column.” But according to ForumExample (  
> [http://wiki.rubyonrails.org/rails/pages/ForumExample](http://wiki.rubyonrails.org/rails/pages/ForumExample)) doesn’t always do  
> this. A Message has\_one author but there’s no ‘belongs\_to :message’ in  
> the Author class.

The ForumExample looks wrong. I would expect to see Author has\_many  
:messages and Message has\_one :author.

> Another thing that confused me was the fact that the DBModel page on the  
> wiki says “Note that you’ll never need to label a link |belongs\_to| ?  
> relationships are defined from the source table viewpoint, so the  
> |belongs\_to| is implicit in the relationship.” I wasn’t sure if that’s  
> just referring to use of this particular gem (which I can’t get to work,  
> but I’ve managed without) or generally.

That’s specific to the use of the dbmodel gem.

regards

Justin

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 24, 2006, 2:33am UTC](https://www.ruby-forum.com/t/when-not-to-use-belongs-to/86404/4 "2006-03-24T02:33:14Z")

</div>

Justin F. wrote:

> The ForumExample looks wrong. I would expect to see Author has\_many  
> :messages and Message has\_one :author.  
> ^^^^^^^ Oops! I meant belongs\_to
