Class/model hierarchy for a school-management application Please find how I would model a system that had teachers, students, classes and parents. Instead of creating a model for each user type, I created only two models, User and Profile. Each user can have multiple profiles and each profile can have a unique profile_type.

I would love to get some feedback on my proposed architecture.

class User &lt; ActiveRecord::Base
  has_many :profiles
  has_many :user_klasses
  has_many :klasses, through: :user_klasses

  def get_student_classes
    student_profile = profiles.select { |p| p.profile_type == 'student' }.first
    return [] if student_profile.nil?

    return klasses.select { |k| k.profile_id == student_profile.id }
  end

  def get_teacher_classes
    teacher_profile = profiles.select { |p| p.profile_type == 'teacher'}.first
    return [] if teacher_profile.nil?

    return klasses.select { |k| k.profile_id == teacher_profile.id }
  end

end

class Klass &lt; ActiveRecord::Base
  has_many :user_klasses
  has_many :users, through: :user_klasses
  belongs_to :teacher, class_name: 'Profile', -&gt; { where profile_type: 'teacher' },
                        dependent: :destroy
end

class UserKlasses &lt; ActiveRecord::Base
  belongs_to :user
  belongs_to :klass
end

class Profile &lt; ActiveRecord::Base
  belongs_to :user
end
You mention that you would rather not create extra models but I would consider creating a model for Teacher, Student and for Parent. It is not really that much more work but will probably keep the separation between the concepts much cleaner in the long run (as per Damien's comments). I personally would also use names like TeacherRole and ParentRole or TeacherFacet.

I would also consider keeping the cardinality as has_one unless you need to deal with the possibility that a user needs more than one teacher profile (possibly because they teach in more than one places) or more than one parent profile (maybe they have multiple children).

You obviously haven't shown your complete data model but it looks wrong that each user is linked to classes through the user_klasses relation but then you query classes using a profile_id field on the Klass. Firstly you probably want to query the profile_id on the UserKlass model (the profile_id on Klass looks like it refer to the teacher's profile) but you should probably put the klasses relation on the Profile model (or whatever you call it).

One other thing I noticed here:

  belongs_to :teacher, class_name: 'Profile', -&gt; { where profile_type: 'teacher' },
                    dependent: :destroy


You probably don't want to remove the teacher record if a class is deleted, they may be teaching other classes after all. However you probably want to delete UserKlass if the Klass (or User for that matter) is deleted. Likewise it would make sense to delete the Profile record when a User is deleted.