Skip to content

Instantly share code, notes, and snippets.

@masterkain
Created November 5, 2010 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masterkain/664081 to your computer and use it in GitHub Desktop.
Save masterkain/664081 to your computer and use it in GitHub Desktop.
class Task < ActiveRecord::Base
belongs_to :category
belongs_to :project
validates_presence_of :project_id, :category_id
validates_with OvertimeValidator
scope :of_date, lambda { |date| where(:date => date) }
end
class OvertimeValidator < ActiveModel::Validator
def validate(record)
record.errors[:overtime] << "No more than 8 hours a day, please" if Task.of_date(self.date).map(&:amount).sum + self.amount > 8
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment