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 --

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?

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

ceicke wrote:

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?

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 --