Skip to content

Instantly share code, notes, and snippets.

@sj26
Last active August 29, 2015 14:07
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 sj26/9c7e291fc35486048cfa to your computer and use it in GitHub Desktop.
Save sj26/9c7e291fc35486048cfa to your computer and use it in GitHub Desktop.
Business rule validation
# CREATE TABLE users (id INT NOT NULL AUTO_INCREMENT, age INT UNSIGNED, PRIMARY KEY id)
class User < ActiveRecord::Base
# column gives us age / age=
validates :age, presence: true, may_serve_alcohol: true
end
class MayServeAlcoholValidator < ActiveModel::EachValidator
def validate_each(model, attribute, value)
if model[attribute].present? && model[attribute] < 18
model.errors.add attribute, "must be over 18 to serve as bar staff"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment