Hello, I have a couple of questions: 1. How to implement IS A relationship I have an events table and model person table and model Now events contain speakers and speakers are people. So how to implement is a relationship, like speaker IS A person I DO NOT have speaker table and model. Is there a need to create it. 2. For the time being, I have defined has_many :through relationship between people and events. In the join table I want to store the TOPIC that the speaker will speak in the event. So how can i store it from the form. so the join table should look like person_id event_id Topic Thank you. -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
AndyV
2008-May-20 16:41 UTC
Re: IS A relationship in rails and additional field in Join tabl
The answer depends on whether or not ''speaker'' is a special kind of person that has certain attributes and abilities that only a speaker has. If that is the case then you can subclass using Single Table Inheritance by adding a ''type'' column to your people table and implementing the Speaker class like this: class Person < ActiveRecord::Base ..general person kinds of things... end class Speaker < Person ...special speaker stuff... end The "<" in the class definition indicates it''s inheritance. And, yes, that means Person IS A extension of the ActiveRecord::Base class. Now, if your intention is only to say "the speaker at this event is that particular person over there" then you can do this: class Event < ARec::Base belongs_to :speaker, :class_name=>''Person'' end As show you''ll need a ''speaker_id'' column on the event. You could also make it ''person_id'' if you prefer, but you''ll need to add :foreign_key=>''person_id'' to the belongs_to invocation. Either way, this means you can call the person associated with the event the ''speaker'' in the context of the Event. On May 20, 11:02 am, Ank Ag <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello, > I have a couple of questions: > 1. How to implement IS A relationship > > I have an events table and model > person table and model > Now events contain speakers and speakers are people. > So how to implement is a relationship, like speaker IS A person > I DO NOT have speaker table and model. Is there a need to create it. > > 2. For the time being, I have defined has_many :through relationship > between people and events. In the join table I want to store the TOPIC > that the speaker will speak in the event. So how can i store it from the > form. > > so the join table should look like > person_id event_id Topic > Thank you. > -- > Posted viahttp://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---