Created
June 14, 2011 18:08
-
-
Save Sephi-Chan/1025485 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Training Load (0.2ms) SELECT `trainings`.* FROM `trainings` WHERE ((begin_at <= '2011-06-15 09:00:00' AND '2011-06-15 09:00:00' <= finish_at) | |
OR (begin_at <= '2011-06-15 11:00:00' AND '2011-06-15 11:00:00' <= finish_at)) | |
Training Load (0.2ms) SELECT `trainings`.* FROM `trainings` WHERE `trainings`.`trainer_id` = 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Training < ActiveRecord::Base | |
belongs_to :service | |
belongs_to :trainer, :class_name => 'User' | |
belongs_to :location | |
has_many :participations | |
has_many :participants, :through => :participations | |
validate :validates_trainer_availability | |
validate :validates_location_availability | |
def self.between(begin_at, finish_at) | |
Training.where( | |
'(begin_at <= :begin_at AND :begin_at <= finish_at) | |
OR (begin_at <= :finish_at AND :finish_at <= finish_at)', | |
:begin_at => begin_at, | |
:finish_at => finish_at | |
) | |
end | |
private | |
def validates_trainer_availability | |
trainings = (Training.between(self.begin_at, self.finish_at) & Training.where(:trainer_id => trainer_id)) | |
errors.add(:trainer_id, :trainer_already_used) if trainings.any? | |
end | |
def validates_location_availability | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment