Skip to content

Instantly share code, notes, and snippets.

@sovanlandy2
Created September 25, 2014 06:09
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 sovanlandy2/5f2e4b9bfd6410c6bdc7 to your computer and use it in GitHub Desktop.
Save sovanlandy2/5f2e4b9bfd6410c6bdc7 to your computer and use it in GitHub Desktop.
RoR Validations
# Validate the length of the string
validates_length_of :username, :minimum => 5, :maximum => 255, :allow_blank => false
# or
validates :password, :length => { :minimum => 6 }, allow_blank: true
# Validate paperclip attachment
validates_attachment :profile_image,
:content_type => { :content_type => ["image/jpg", "image/jpeg", "image/png", "image/bmp", "image/gif"] },
:size => { :in => 0..10.megabytes }
# Custom Validation
validate :cities_validation
def cities_validation
if default_city_id.present? && (!City.pluck(:id).include? default_city_id)
errors.add(:default_city_id, "default_city_id is not valid.")
end
end
# uniqueness validation on one attr
validate :facebook_id, :uniqueness => true
# Before Validation Callback
before_validation(:on => :create) { |user| user.email = email.downcase rescue nil }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment