# Has\_one vs. Belongs\_to?

**URL:** <https://www.ruby-forum.com/t/has-one-vs-belongs-to/92364>\
**Category:** Rails\
**Created:** [April 18, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/has-one-vs-belongs-to/92364 "2007-04-18T18:11:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 18, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/has-one-vs-belongs-to/92364/1 "2007-04-18T18:11:35Z")

</div>

I have clients, (table - model-controller) and I have plans (table -  
model - controller). A client can have several plans and a plan can  
only map to one client. I would assume that in the model for clients  
i need a has\_many and in the model for plans I need either a has\_one  
or a belongs\_to. I am not sure which to use and obviousely it is not  
entirely clear to me (after reading in agile web developement) what  
these methods are doing for me, other than it seems to be obviouse i  
should have these relationships in place.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 18, 2007, 6:28pm UTC](https://www.ruby-forum.com/t/has-one-vs-belongs-to/92364/2 "2007-04-18T18:28:35Z")

</div>

belongs\_to would look like this:

clients (table)  
id  
name  
etc

plans (table)  
id  
client\_id  
name

client.rb (model)  
has\_many :plans

plan.rb (model)  
belongs\_to :client

has\_one is just like has\_many, but it restricts the selection.

clients (table)  
id  
name  
plan\_id  
etc

plans (table)  
id  
name

client.rb (model)  
belongs\_to :plan

plan.rb (model)  
has\_one :client

The first example is probably what you want (one to many) and the  
second  
example is probably not what you want (one-to-one).

belongs\_to is used on the model that contains the foreign key. If the  
plans table has the client\_id foreign key, the plan belongs\_to :client  
and  
the client has\_many :plans. (see how that makes sense when you say it  
out  
loud?) It doesn’t make as much sense to say that a client belongs\_to  
a  
plan though.

Hopefully I helped clear it up a bit instead of confusing you more.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 18, 2007, 8:07pm UTC](https://www.ruby-forum.com/t/has-one-vs-belongs-to/92364/3 "2007-04-18T20:07:57Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I have clients, (table - model-controller) and I have plans (table -  
> model - controller). A client can have several plans and a plan can  
> only map to one client. I would assume that in the model for clients  
> i need a has\_many and in the model for plans I need either a has\_one  
> or a belongs\_to. I am not sure which to use and obviousely it is not  
> entirely clear to me (after reading in agile web developement) what  
> these methods are doing for me, other than it seems to be obviouse i  
> should have these relationships in place.

belongs\_to always goes in the model that has the foreign key in its  
table. Client would have a plan\_id field, so it belongs\_to :plan.

This is what I wrote as “required reading” for associations and basic  
relationships.

[http://blog.hasmanythrough.com/2007/1/15/basic-rails-association-cardinality](http://blog.hasmanythrough.com/2007/1/15/basic-rails-association-cardinality)

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 18, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/has-one-vs-belongs-to/92364/4 "2007-04-18T20:13:47Z")

</div>

thank you both, super helpful  
JE

On Apr 18, 2:07 pm, Josh S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
