Skip to content

Instantly share code, notes, and snippets.

@seanreed1111
Last active August 29, 2015 13:56
Show Gist options
  • Save seanreed1111/8862800 to your computer and use it in GitHub Desktop.
Save seanreed1111/8862800 to your computer and use it in GitHub Desktop.
Model validation in rails
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