Question

I'm wondering what the best way would be to set up models in Ruby on Rails for a tutoring website.

I would like the users to register (I have no particular way in mind but I'm assuming I'll go with one of the more popular ruby gems). They can then choose to be either tutors or students or both. Should I make a tutors model, a students model and have them inherit basic info from the authentication? Or would it be better to have a users model where all the basic info goes (birthday, gender) and then have student/tutor inherit from that?

Was it helpful?

Solution 2

I think it is better for Student and Tutor to inherit from your User model. You can choose to still keep the data in the same database table using STI in rails.

This approach will ensure that there is a clear separation of responsibility in your domain, while re-using the same authentication (and later authorization) flow.

OTHER TIPS

I would have a User model with the basic information and then something like this:

class User
  has_many :course_students
  has_many :student_courses, through: :course_students, class_name: "Course"

  has_many :course_tutors
  has_many :tutored_courses, through: :course_tutors, class_name: "Course"

end

class Course
  has_many :course_students
  has_many :students, through: :course_students, class_name: "User"

  has_many :course_tutors
  has_many :tutors, through: :course_tutors, class_name: "User"
end

class CourseStudent
  belongs_to :course
  belongs_to :student, class_name: "User"
end

class CourseTutor
  belongs_to :course
  belongs_to :tutor, class_name: "User"
end

This way a user can easily be a tutor and student and just has to have the shared information. I'd probably insert dedicated tutor/student models, if the need arises.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top