Skip to content

Instantly share code, notes, and snippets.

@sameera207
Created December 18, 2012 07:28
Show Gist options
  • Select an option

  • Save sameera207/4325833 to your computer and use it in GitHub Desktop.

Select an option

Save sameera207/4325833 to your computer and use it in GitHub Desktop.
ROR validation callbacks
module (ROR validation callbacks)
== acceptance
class Person < ActiveRecord::Base
validates :terms_of_service, :acceptance => true
end
== validates_associated (* should not used on both ends)
class Library < ActiveRecord::Base
has_many :books
validates_associated :books
end
== confirmation (* finds for a column with appending text <column>_confirmation)
class Person < ActiveRecord::Base
validates :email, :confirmation => true
end
== exclusion
class Account < ActiveRecord::Base
validates :subdomain, :exclusion => { :in => %w(www us ca jp),
:message => "Subdomain %{value} is reserved." }
end
== format
validates :legacy_code, :format => { :with => /\A[a-zA-Z]+\z/,
:message => "Only letters allowed" }
== inclusion
class Coffee < ActiveRecord::Base
validates :size, :inclusion => { :in => %w(small medium large),
:message => "%{value} is not a valid size" }
end
== length
class Person < ActiveRecord::Base
validates :name, :length => { :minimum => 2 }
validates :bio, :length => { :maximum => 500 }
validates :password, :length => { :in => 6..20 }
validates :registration_number, :length => { :is => 6 }
end
== presence
class LineItem < ActiveRecord::Base
belongs_to :order
validates :order_id, :presence => true
end
== uniqueness
class Account < ActiveRecord::Base
validates :email, :uniqueness => true
end
== validates_with
class Person < ActiveRecord::Base
validates_with GoodnessValidator
end
class GoodnessValidator < ActiveModel::Validator
def validate(record)
if record.first_name == "Evil"
record.errors[:base] << "This person is evil"
end
end
end
== allow_nil, allow_blank, on
class Person < ActiveRecord::Base
# it will be possible to update email with a duplicated value
validates :email, :uniqueness => true, :on => :create
end
== conditional validatons
class Order < ActiveRecord::Base
validates :card_number, :presence => true, :if => :paid_with_card?
def paid_with_card?
payment_type == "card"
end
end
== Grouping validations
class User < ActiveRecord::Base
with_options :if => :is_admin? do |admin|
admin.validates :password, :length => { :minimum => 10 }
admin.validates :email, :presence => true
end
end
== custom validations
class MyValidator < ActiveModel::Validator
def validate(record)
unless record.name.starts_with? 'X'
record.errors[:name] << 'Need a name starting with X please!'
end
end
end
class Person
include ActiveModel::Validations
validates_with MyValidator
end
==== validations errors =========
class Person < ActiveRecord::Base
def a_method_used_for_validation_purposes
errors.add(:name, "cannot contain the characters !@#%*()_-+=")
end
end
person = Person.create(:name => "!@#")
person.errors[:name]
# => ["cannot contain the characters !@#%*()_-+="]
person.errors.full_messages
# => ["Name cannot contain the characters !@#%*()_-+="]
http://guides.rubyonrails.org/active_record_validations_callbacks.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment