Created
September 25, 2014 06:09
-
-
Save sovanlandy2/5f2e4b9bfd6410c6bdc7 to your computer and use it in GitHub Desktop.
RoR Validations
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
# 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