这就是我需要的:
学校有很多老师
教师可以属于一所或多所学校
主题可以属于一个或多个教师
教师可以有很多科目
我如何建立一个存储关系:
老师A在1号学校和2号学校工作,但是他在1号学校教数学和物理,在学校2只学数学?
这就是我已经做过的事情:
class School < ApplicationRecord
has_many :school_teachers
has_many :teachers, through: :school_teachers
end
class Teacher < ApplicationRecord
has_many :school_teachers
has_many :schools, through: :school_teachers
end
class SchoolTeacher < ApplicationRecord
belongs_to :school
belongs_to :teacher
end
class Subject < ApplicationRecord
end
答案 0 :(得分:3)
由于受试者与一个或多个学校相关联。老师配对,然后我会建议这可能真的是一个3路&#34;已经属于很多&#34;关系。
class School < ApplicationRecord
has_many :school_teacher_subjects
has_many :teachers, through: :school_teacher_subjects
has_many :subjects, through: :school_teacher_subjects
end
class Teacher < ApplicationRecord
has_many :school_teacher_subjects
has_many :schools, through: :school_teacher_subjects
has_many :subjects, through: :school_teacher_subjects
end
class Subject < ApplicationRecord
has_many :school_teacher_subjects
has_many :schools, through: :school_teacher_subjects
has_many :teachers, through: :school_teacher_subjects
end
class SchoolTeacherSubject < ApplicationRecord
belongs_to :school
belongs_to :teacher
belongs_to :subject
end
以下是客户,电话和地址的3路HABTM关系示例:Custom db entry for 3 way habtm in ROR 。