你可以有类似下面的模型。
class Doctor
has_many :specialties, :through => :practices
has_many : enrollments
has_many :programs , :through => : enrollments
end
class Program
has_many : enrollments
has_many :doctors, :through => : enrollments
belongs_to :specialty
end
class Enrollment
belongs_to : doctor
belongs_to :program
end
class Specialty
has_many :practices
has_many :doctors, :through => :practices
has_many :programs
end
class Practice
belongs_to :doctor
belongs_to :specialty
end
希望能帮助到你。
更新
如果医生只能通过程序获得专业知识,则可以这样建模。
class Doctor
has_many :enrollments
has_many :programs, :through => :enrollments
end
class Program
has_many :enrollments
has_many :doctors, :through => :enrollments
belongs_to :specialty
end
class Enrollment
belongs_to :doctor
belongs_to :program
end
class Specialty
has_many :programs
has_many :enrollments , :through => :programs
end
获得所有专业的医生,例如神经病学。
@neurology.enrollments.collect { |c| c.doctor }.uniq
或者
Doctor.includes(:enrollments).where(:enrollments => {:specialty_id => @neurology.id})
要获得医生的所有专业,您必须这样做。
@doctor.programs.collect { |p| p.specialty }.uniq