Skip to content

Instantly share code, notes, and snippets.

@jmazzi
Created April 23, 2009 17:27
Show Gist options
  • Save jmazzi/100619 to your computer and use it in GitHub Desktop.
Save jmazzi/100619 to your computer and use it in GitHub Desktop.
class Foo < ActiveRecord::Base
# attrs: bar (integer), baz (integer)
# want to make sure Foo.baz is > than Foo.bar
# this would only run validation when baz is numeric (because any int is > nil)
validates_each :bar do |record, attr, value|
if record.baz.is_a?(Numeric)
if value.is_a?(Numeric)
record.errors.add attr, 'must be less than baz.' unless value < record.baz
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment