I have a teacher model which is inherited from user model and a separate class model. Now I need to set up many_to_many
relationship between teacher model and class model.
How can i do this, I'm little confused?
I have a teacher model which is inherited from user model and a separate class model. Now I need to set up many_to_many
relationship between teacher model and class model.
How can i do this, I'm little confused?
See http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
You need an intermediate table to map teachers to classes.
class Teacher < User
has_many :teacher_classes
has_many :classes, through: :teacher_classes
end
class TeacherClass < ApplicationRecord
belongs_to :teacher
belongs_to :class
end
class Class < ApplicationRecord
has_many :teacher_classes
has_many :teachers, through: :teacher_classes
end
You will need to change Class
to a name that does not conflict with the builtin ruby Class
.