I've been trying to solve this problem for a while now. Looked here, but it is not exactly what I need.
I have three models: User, Group, GroupMembership. User can be a teacher and a student, so user has different Roles through UserRoles table. Groups can have multiple teachers. What I want is something like this:
Role.rb
has_and_belongs_to_many :users #this works fine
User.rb
has_and_belongs_to_many :roles # this works fine
has_many :group_memberships
has_many :groups, through: :group_memberships
has_many :teachers, through: :group_memberships
has_many :students, through: :group_memberships
Group.rb
has_many :students, through: :group_memberships
has_many :teachers, through: :group_memberships
GroupMembership.rb
belongs_to :student
belongs_to :teacher
belongs_to :group
User with roles works fine, there is no problem with the different roles for user. The problem is with group memberships. The above is just something that I want to work, but in some cases I need to supply source or class name and I am not sure what exactly to do. And what kind of migrations should I create?