Hello,
I have a couple of questions:
-
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.
-
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.
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.