Has_many to Has_many scheduling problems

By : Morris
Source: Stackoverflow.com
Question!

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!

By : Morris


Answers

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

By : Jagdish


The simplest, and following the style of the existing code, would be to ask wether the employee wants to calculate his salary or not and make recursive calls:

// Wrap it all inside a function
function askEmployee() {
    // Current code
    var EmployeeName, EmployeeSales, Bonus, Commission;
    EmployeeName = prompt("Please enter your name.");
    EmployeeSales = prompt("Please enter your monthly sales");
    alert(EmployeeName + " your salary this month is" + " $" + EmployeeSales);

    // Ask if should calculate new salary or not, and act accordingly
    var askAgain = confirm("Do you want to calculate your salary for this month?")
    if ( askAgain ) {
        askEmployee();
    }
    else {
        return false
    }
}

// Then call it for the first time
askEmployee()


This video can help you solving your question :)
By: admin