Last active
August 29, 2015 14:07
-
-
Save sj26/9c7e291fc35486048cfa to your computer and use it in GitHub Desktop.
Business rule validation
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
# 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