What is the best way to set up a relationship between two tables where table-1 has a one-to-one relationship with table-2 but table-2 has a one-to-many relationship with table-1? For example, suppose there is an events application (seminars, forums, conferences, etc). Each event has only one address but since many events can occur at the same location throughout the year, a given address can belong to many events. Thanks, Stan --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Jul 21, 2008, at 12:25 AM, stan.baptista wrote:> What is the best way to set up a relationship between two tables where > table-1 has a one-to-one relationship with table-2 but table-2 has a > one-to-many relationship with table-1? > > For example, suppose there is an events application (seminars, forums, > conferences, etc). Each event has only one address but since many > events can occur at the same location throughout the year, a given > address can belong to many events. > > Thanks, > StanThat''s just a normal association: class Event < ActiveRecord::Base belongs_to :address end class Address < ActiveRecord::Base has_many :events end Don''t let the seemingly backward "belongs_to :address" fool you. The ''belongs_to'' association is on the model that contains the foreign key (address_id in this example) even though you say "Each event has only one address..." -- the "has ... one" is misleading in this case. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> Don''t let the seemingly backward "belongs_to :address" fool you. The > ''belongs_to'' association is on the model that contains the foreign key > (address_id in this example) even though you say "Each event has only > one address..." -- the "has ... one" is misleading in this case.Got it. Thanks Rob. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
That sounds like a plain 1:many to me. Address has_many :events, and Event belongs_to :address. -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of stan.baptista Sent: Sunday, July 20, 2008 9:26 PM To: Ruby on Rails: Talk Subject: [Rails] Asymmetrical HABTM(?) What is the best way to set up a relationship between two tables where table-1 has a one-to-one relationship with table-2 but table-2 has a one-to-many relationship with table-1? For example, suppose there is an events application (seminars, forums, conferences, etc). Each event has only one address but since many events can occur at the same location throughout the year, a given address can belong to many events. Thanks, Stan --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---