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?
on 2007-05-04 20:25
on 2007-10-21 19:24
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 Zhan <rails-mailing-l...@andreas-s.net>