# Inherited model relation design

**URL:** <https://www.ruby-forum.com/t/inherited-model-relation-design/103369>\
**Category:** Rails\
**Created:** [May 4, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/inherited-model-relation-design/103369 "2007-05-04T20:25:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [May 4, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/inherited-model-relation-design/103369/1 "2007-05-04T20:25:52Z")

</div>

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  
fields.  
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  
end

class Doctor \< ActiveRecord::Base  
has\_one :person, :as =\> titled\_person  
end

class Lawyer \< ActiveRecord::Base  
has\_one :person, :as =\> titled\_person  
end  
…

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?

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [October 21, 2007, 7:24pm UTC](https://www.ruby-forum.com/t/inherited-model-relation-design/103369/2 "2007-10-21T19:24:10Z")

</div>

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  
end

and

class Teacher \< Person  
end

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 Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
