Search code examples
ruby-on-railsdesign-patternsinheritancesingle-table-inheritance

Rails multiple roles defined in relationship with a third party


I am building a small app which has classes User, Student, Tutor, Meeting.

Each user can be tutor in one Meeting, but student in another meeting. I have been thinking about how to model these relationship.

Right now, I have a User model, a separate Participant model with an attribute Role = "tutor"/"student". Tutor and Student extend Participant using single-table inheritance, and belong to Meeting.

However, I wonder if this is a good design. (It seems to be unintuitive to me).

What would you do in my situation?

Thank you.


Solution

  • It is acceptable but there are other ways that are cleaner. You could design you Meeting model like this:

    has_and_belongs_to_many :users, :as => :studens
    belongs_to :tutor, :foreign_key => 'tutor_id', :class_name => 'User'
    

    Your User Model:

    has_and_belongs_to_many :meetings, :as => :meetings_taken
    has_many :meetings_given, :foreign_key => 'tutor_id', :class_name => 'Meeting'
    

    So you would only need two models.

    // edit

    For propose, accept, decline,.. I would create a Invitation Model user_id:integer (Invited User), meeting_id(Through this you know who is allowed to invite other people because Meeting belongs to the tutor), status:string (waiting, accepted, delicesed might be options) maybe explaination:text (When somebody delices).