Skip to content

Instantly share code, notes, and snippets.

@flakyfilibuster
Created November 14, 2012 03:12
Show Gist options
  • Save flakyfilibuster/4070029 to your computer and use it in GitHub Desktop.
Save flakyfilibuster/4070029 to your computer and use it in GitHub Desktop.
Validations in Rails
# Validate that the price contains a maximum of two decimal places
# Validate the image url is a proper URL and ends with a image file type
# Validate that the size is only one of the following: "small", "medium", "large"
validates :price, :format => { :with => /^\d+??(?:\.\d{0,2})?$/ }
validates :imageurl, :format => { :with => /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\.(png|jpg|gif)?$/ }
validates :size, :inclusion => { :in => %w(small medium large) }
# Validate the presence of the title, price, description, image url, size
# Validate that the title is unique, and less than 50 characters
# Validate that the description is less than 250 words
validates :title, :price, :description, :imageurl, :size, :presence => true
validates :title, :uniqueness => true, :length => { :minimum => 50 }
validates :description, :length => { :maximum => 250 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment