0

当老师设置他/她的可用性时,我在这里设置关联有问题。教师只能针对他/她注册的课程设置可用性。从 CourseType 表中预定义的课程列表。

在教师注册时,用户需要选择教师可以教授的课程。

所以对于教师来说,在设置可用性时,只有那些课程应该是可见的。

class TeacherDetail < UserDetail
  include Mongoid::Document


  has_one :user, as: :user_type

  has_and_belongs_to_many :courses, class_name: "CourseType", inverse_of: :course_type, autosave: true

  accepts_nested_attributes_for :user


end

class CourseType
  include Mongoid::Document
  include Mongoid::Timestamps

  field :type, type: String
  field :name, type: String

  auto_increment :type_id

  has_and_belongs_to_many :teacher_details, class_name: "TeacherDetail", inverse_of: :teacher_id, autosave: true
end

class TeacherAvailibility

  include Mongoid::Document
  include Mongoid::Timestamps

  include RailsAdmin::TeacherAvailabilityRailsAdminConcern

  field :date, type: Date
  field :start_time, type: String
  field :end_time, type: String
  field :cost, type: Float

  belongs_to :teacher_detail

end
4

1 回答 1

0

关系应该是这样的

class TeacherDetail has_many :courses end

class CourseType belong_to :teacher end

class TeacherAvailability has_one :teacherDetail, through: :course_type end

我希望我正确理解了你的问题

但是 mongoDB 不支持 has_many through 。你可以在这里寻找解决方案。

如何实现has_many:通过与Mongoid和mongodb的关系?

于 2015-05-09T09:26:07.520 回答