Last active
August 29, 2015 13:56
-
-
Save seanreed1111/8862800 to your computer and use it in GitHub Desktop.
Model validation in rails
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
class Person < ActiveRecord::Base | |
attr_accessible :username, :password, :age, :graduation_year | |
#must have first and last name. | |
#If not present, throw an error message (optional) | |
validates_presence_of :username | |
validates_presence_of :password | |
validates_length_of :password | |
validates_format_of :password, | |
:with => /[0-9]/ | |
# Use :scope if the combination of username/password must be unique. | |
validates_uniqueness_of :username, | |
:case_sensitive => false, | |
:scope => [:password] | |
#age ain't nothing but a number | |
validates_numericality_of :age, | |
:allow_nil => true | |
#so is graduation year | |
validates_numericality_of :graduation_year, | |
:allow_nil => true, | |
:greater_than => 2012, | |
:less_than_or_equal_to => Time.now.year, | |
:only_integer => true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment