Skip to content

Instantly share code, notes, and snippets.

@nbibler
Created December 20, 2010 12:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nbibler/748350 to your computer and use it in GitHub Desktop.
Save nbibler/748350 to your computer and use it in GitHub Desktop.
An ActiveModel::EachValidator to check local attribute value against "project" value.
en:
errors:
messages:
project_effort_exceeded: 'cannot be larger than project effort'
class Task < ActiveRecord::Base
belongs_to :project
validates :effort => true, :numericality => {:greater_than_or_equal_to => 1}
end
class TaskEffortValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if record.project && record.project.effort > value
record.errors.add(attribute, :project_effort_exceeded, options)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment