Has_many to Has_many scheduling problems

35 Views Asked by At

I have two models. One model is LessonHours, which is responsible for guiding the school classes. This model has weekday, start time and end time fields. The school's admin will register each lesson hour. For example, on monday, there'll be 5 classes, beginning at 8 AM, 8:50 AM, 9:40 AM and 10:30 AM. There'll be a register for each class.

The other model I have is the Teacher model. When we register the teacher, I need to tell the application what is his availability. So I need to show all Lesson Hours, divided by weekdays, so the admin can check its avaiability (ex: monday: 8 AM, 8:50 AM, tuesday: 10:30 AM, etc)

What's the best way to do it?

Thanks in advance!

1

There are 1 best solutions below

4
Jagdish On

I guess this can be achieved by has_many :through Association concept

class Teacher < ActiveRecord::Base
 has_many :schedules
 has_many :lesson_hours, through: :schedules
end

class Schedules < ActiveRecord::Base
 belongs_to :teacher
 belongs_to :lesson_hour
end

class LessonHour < ActiveRecord::Base
  has_many :schedules
  has_many :teachers, through: :schedules
end

For more example please refer http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association