Forum: Ruby on Rails inherited model relation design

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
C91098dc76d7ad473165ef24fe805312?d=identicon&s=25 Nanyang Zhan (xain)
on 2007-05-04 20:25
I want to have a Person class
It will has attributes like name,email,address, phone...
Then I will like some subclasses like Student, Teacher, Doctor, Lawyer,
JobSeeker... these models will have attributes that belong to related
As a person may be a teacher and a lawyer at the same time, a Person
class will be allowed to has more than one subclasses.

I have checked out Polymorphic Associations,
it is done like this:
class Person < ActiveRecord::Base
 belongs_to :titled_person :polymorphic => true

class Doctor < ActiveRecord::Base
 has_one :person, :as => titled_person

class Lawyer < ActiveRecord::Base
 has_one :person, :as => titled_person

but It deal with a class with ONE subclass, and the words(has, belongs
to) totally mess up my idea.

can any one to help me to do it right?
F6834c5f9d9df5d3491173bf00c3261f?d=identicon&s=25 mkumba (Guest)
on 2007-10-21 19:24
(Received via mailing list)
I have a similar, and even simpler problem.

I would be fine right now with single inheritance in my domain model.

I want to do the equivalent of

class Person < ActiveRecord::Base


class Teacher < Person

I just built the usual scaffolding.  I can create a person, but when I
try to list Teachers I get a nomethod error, citing one of the Persons
doesn't have one of the methods expected of a teacher.

I suspect that I have to hand code the subclass/superclass
relationship in the db:migrate, but this hardly seems in the spirit of
this environment that is supposed to be helping me along the way.

Back to your problem:

The classic way to solve the multiple classification issue you have is
have two classes that are peers (Person and Role).  Role belongs to
Person and Person has many roles.  You then sub type role for teacher,
student etc.

When you get there you will have the same problem I have, although I
think it is a much simpler problem, that I will probably find the
answer to later today.
The classic way to

On May 4, 12:25 pm, Nanyang Zhan <>
This topic is locked and can not be replied to.