# Has\_one confusion

**URL:** <https://www.ruby-forum.com/t/has-one-confusion/82594>\
**Category:** Rails\
**Created:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/has-one-confusion/82594 "2007-01-19T16:31:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ceicke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c0e974/32.png) [@ceicke](https://www.ruby-forum.com/u/ceicke)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/has-one-confusion/82594/1 "2007-01-19T16:31:32Z")

</div>

Hi!

I am very new to Ruby on Rails and have thus far developed web  
applications in PHP. I have a problem applying what I have learned  
about has\_one relations to an existing database. The database looks as  
follows

Table: people  
id  
first\_name  
last\_name  
citizenship\_jn\_id  
home\_country\_jn\_id  
travel\_country\_jn\_id

Table: countries  
id  
country\_name\_en  
iso\_country\_code

The fields citizenship\_jn\_id, home\_country\_jn\_id and  
travel\_country\_jn\_id are all foreign keys to countries table. I have  
tried to model this in the Person class as follows:

class Person \< ActiveRecod::Base  
has\_one :citizenship, :class\_name =\> “Country”, :foreign\_key =\>  
“citizenship\_jn\_id”  
has\_one :home\_country, :class\_name =\> “Country”, :foreign\_key =\>  
“home\_country\_jn\_id”  
has\_one :travel\_country\_jn\_id, :class\_name =\> “Country”, :foreign\_key  
=\> “travel\_country\_jn\_id”  
end

But this is not correct as the wrong SQL is produced. What would be  
correct to have the right associations in the model?

Thanks for helping a newbe!  
Christoph

---

<div class="post-metadata">

**Author:** ![ceicke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c0e974/32.png) [@ceicke](https://www.ruby-forum.com/u/ceicke)\
**Post date:** [September 25, 2007, 10:33pm UTC](https://www.ruby-forum.com/t/has-one-confusion/82594/2 "2007-09-25T22:33:40Z")

</div>

Hi –

On Fri, 12 Jan 2007, ceicke wrote:

> first\_name  
> The fields citizenship\_jn\_id, home\_country\_jn\_id and  
> end
> 
> But this is not correct as the wrong SQL is produced. What would be  
> correct to have the right associations in the model?

You’ve got the logic reversed: in the has/belongs\_to relationships,  
the foreign key goes in the thing that belongs\_to the other thing.

In other words, if garage belongs to house, you’d have:

class House \< ARB  
has\_one :garage # or has\_many :garages  
end

class Garage \< ARB  
belongs\_to :house  
end

and then in the garages table:

id  
house\_id

So, in your case, you’d want:

class Person \< ARB  
belongs\_to :citizenship,  
:class\_name “Country”, :foreign\_key =\> “citizenship\_jn\_id”

etc.

It doesn’t sound semantically right, in this case… but technically  
that’s the direction the logic flows in.

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![ceicke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c0e974/32.png) [@ceicke](https://www.ruby-forum.com/u/ceicke)\
**Post date:** [September 25, 2007, 10:37pm UTC](https://www.ruby-forum.com/t/has-one-confusion/82594/3 "2007-09-25T22:37:39Z")

</div>

ceicke wrote:

> last\_name  
> travel\_country\_jn\_id are all foreign keys to countries table. I have
> 
> But this is not correct as the wrong SQL is produced. What would be  
> correct to have the right associations in the model?

You want to use belongs\_to for those. has\_one is when there is a  
one-one relationship between the objects, belongs is a many-one (one  
country has many users/columns)

---

<div class="post-metadata">

**Author:** ![ceicke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c0e974/32.png) [@ceicke](https://www.ruby-forum.com/u/ceicke)\
**Post date:** [September 25, 2007, 10:46pm UTC](https://www.ruby-forum.com/t/has-one-confusion/82594/4 "2007-09-25T22:46:17Z")

</div>

Hi –

On Fri, 12 Jan 2007, Michael wrote:

> > Table: people  
> > iso\_country\_code  
> > has\_one :travel\_country\_jn\_id, :class\_name =\> “Country”, :foreign\_key  
> > =\> “travel\_country\_jn\_id”  
> > end
> > 
> > But this is not correct as the wrong SQL is produced. What would be  
> > correct to have the right associations in the model?
> 
> You want to use belongs\_to for those. has\_one is when there is a  
> one-one relationship between the objects, belongs is a many-one (one  
> country has many users/columns)

Actually belongs\_to serves for both has\_one and has\_many. The  
“belonger” doesn’t know or need to know how many other things belong  
to the same “haver”.

But ceicke’s Person class does need belongs\_to, rather than has\_one  
(see my earlier post).

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
