需要帮助从多个表Rails4 mongoId设置关联

时间:2015-05-09 08:40:38

标签: ruby-on-rails model-associations mongoid4 rails-4-2-1

我在教师设置他/她的可用性时遇到了设置关联的问题。教师只能针对他/她注册的课程设置可用性。从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

1 个答案:

答案 0 :(得分: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。你可以在这里寻找解决方案。

How to implement has_many :through relationships with Mongoid and mongodb?