Has_one confusion

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

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)
(See what readers are saying! 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)

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)

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)
(See what readers are saying! 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)