我的一些模特:
class Provider < ActiveRecord::Base
has_many :courses, dependent: :destroy
has_many :teachers, dependent: :destroy
end
class Course < ActiveRecord::Base
has_many :teachers, through: :staff_profiles
has_many :staff_profiles, dependent: :destroy
belongs_to :provider
belongs_to :staff_profile
end
class Teacher < ActiveRecord::Base
has_many :courses, through: :staff_profiles
has_many :staff_profiles, dependent: :destroy
belongs_to :provider
end
在课程编辑视图中,我希望让所有提供者的教师让用户选择哪些教师属于该课程。
最简单的做法是什么?
答案 0 :(得分:0)
你可以这样做:
# app/controller/courses_controller.rb
def edit
@course = Course.find(params[:id]) # find the course here
@provider_teachers = @course.provider.teachers
end
另外,在belongs_to :provider
模型中添加Teacher
关联,在provider_id
表格中添加相应的外键(teachers
)。