# Belongs\_to or has\_many

**URL:** <https://www.ruby-forum.com/t/belongs-to-or-has-many/160992>\
**Category:** Rails\
**Created:** [February 21, 2009, 10:39pm UTC](https://www.ruby-forum.com/t/belongs-to-or-has-many/160992 "2009-02-21T22:39:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [February 21, 2009, 10:39pm UTC](https://www.ruby-forum.com/t/belongs-to-or-has-many/160992/1 "2009-02-21T22:39:58Z")

</div>

2 tables Items and Categories

Categories (id, name)  
Items (id, name, category\_id)

Category\_id can be null, and there are Categories that has not an Item.

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [February 21, 2009, 10:44pm UTC](https://www.ruby-forum.com/t/belongs-to-or-has-many/160992/2 "2009-02-21T22:44:30Z")

</div>

Any chance of taking this list moderated?

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [February 22, 2009, 12:41am UTC](https://www.ruby-forum.com/t/belongs-to-or-has-many/160992/3 "2009-02-22T00:41:58Z")

</div>

On Feb 21, 9:39 pm, Jo Jo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 2 tables Items and Categories
> 
> Categories (id, name)  
> Items (id, name, category\_id)
> 
> Category\_id can be null, and there are Categories that has not an Item.

Well you may not realise it yet, but the fact that having a  
category\_id on the items table and no other columns on the categories  
table works for you means that you have already solved this problem:  
the belongs\_to always lives on the table with the foreign key and the  
has\_many or has\_one on the other side.

Fred

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [February 22, 2009, 12:41am UTC](https://www.ruby-forum.com/t/belongs-to-or-has-many/160992/4 "2009-02-22T00:41:58Z")

</div>

On Feb 21, 11:34 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> category\_id on the items table and no other columns on the categories  
> table works for you means that you have already solved this problem:  
> the belongs\_to always lives on the table with the foreign key and the  
> has\_many or has\_one on the other side.

Oops, forgot to add that

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

and [rails.info](http://guides.rails.info/association_basics.html) cover this sort  
of stuff.

Fred
