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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Jan-12 15:36 UTC
Re: has_one confusion
Hi -- On Fri, 12 Jan 2007, 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''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 (http://www.manning.com/black) (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) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Jan-12 16:15 UTC
Re: has_one confusion
Hi -- On Fri, 12 Jan 2007, Michael wrote:> > > 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)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 (http://www.manning.com/black) (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) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---