0

I have issue with setting association here while a Teacher sets his/her availability. A teacher can only set availability against the courses he/she is registered for. List of Courses in pre-defined from CourseType table.

At time of teacher registration, user need to select the courses a teacher can teach.

So For teacher while setting availability, only those courses should be visible.

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
Ankush Ganatra
  • 500
  • 5
  • 23

1 Answers1

0

Relationship should be something like this

class TeacherDetail has_many :courses end

class CourseType belong_to :teacher end

class TeacherAvailability has_one :teacherDetail, through: :course_type end

I hope i understood your question correctly

But mongoDB doesnt support has_many through . you can look for solution here.

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

Community
  • 1
  • 1
Hemali
  • 465
  • 4
  • 8
  • 1) A teacher can set multiple availability for various courses. 2) I am using mongodb, so it wont work with it. – Ankush Ganatra May 09 '15 at 09:37
  • oops i forgot mongodb contraints. i found answer in this post. hope it helps. Steve answer i feel will work for u.http://stackoverflow.com/questions/7000605/how-to-implement-has-many-through-relationships-with-mongoid-and-mongodb – Hemali May 09 '15 at 09:45
  • (Sorry I am relatively new to rails) So not sure if that solves my problem completely. With that I am still not sure about is, how can I only set few of those course types which were associated to the Teacher. – Ankush Ganatra May 09 '15 at 10:05
  • So in this case, a Teacher can teach 5 different languages, but while setting availability, he can say.. I am available 12:00 PM to 1:00 PM for A course but for B course I am only available for half an hour on some other day.. – Ankush Ganatra May 09 '15 at 10:10
  • Here I want user to select only those courses for which he is registered for. – Ankush Ganatra May 09 '15 at 10:16
  • can this be useful.1)Teacher has many course 2)course belong_to teacher - By this relationship when u do teacher.courses.you will get all courses teacher is registered for, 3)course has_many availabilities 4) Availability belongs to teacher and course.. This way. you can fetch which teacher is available at what time for which course – Hemali May 09 '15 at 10:30
  • `courses = Teacher.find(1).courses` this will give u teachers registered course `courses.each do |course| course.availability end` This will give you particular course and particular teachers availabiliity – Hemali May 09 '15 at 10:32