validates

validates(*attributes) Instance Public methods This method is a shortcut to all default validators and any custom validator classes ending in 'Validator'. Note that Rails default validators can be overridden inside specific classes by creating custom validator classes in their place such as PresenceValidator. Examples of using the default rails validators: validates :terms, acceptance: true validates :password, confirmation: true validates :username, exclusion: { in: %w(admin super

validate

validate(*args, &block) Instance Public methods Adds a validation method or block to the class. This is useful when overriding the validate instance method becomes too unwieldy and you're looking for more descriptive declaration of your validations. This can be done with a symbol pointing to a method: class Comment include ActiveModel::Validations validate :must_be_friends def must_be_friends errors.add(:base, 'Must be friends to leave a comment') unless commenter.f

clear_validators!

clear_validators!() Instance Public methods Clears all of the validators and validations. Note that this will clear anything that is being used to validate the model for both the validates_with and validate methods. It clears the validators that are created with an invocation of validates_with and the callbacks that are set by an invocation of validate. class Person include ActiveModel::Validations validates_with MyValidator validates_with OtherValidator, on: :create valid

attribute_method?

attribute_method?(attribute) Instance Public methods Returns true if attribute is an attribute method, false otherwise. class Person include ActiveModel::Validations attr_accessor :name end User.attribute_method?(:name) # => true User.attribute_method?(:age) # => false

before_validation

before_validation(*args, &block) Instance Public methods Defines a callback that will get called right before validation happens. class Person include ActiveModel::Validations include ActiveModel::Validations::Callbacks attr_accessor :name validates_length_of :name, maximum: 6 before_validation :remove_whitespaces private def remove_whitespaces name.strip! end end person = Person.new person.name = ' bob ' person.valid? # => true person.name # =&

after_validation

after_validation(*args, &block) Instance Public methods Defines a callback that will get called right after validation happens. class Person include ActiveModel::Validations include ActiveModel::Validations::Callbacks attr_accessor :name, :status validates_presence_of :name after_validation :set_status private def set_status self.status = errors.empty? end end person = Person.new person.name = '' person.valid? # => false person.status # => false

lookup_ancestors

lookup_ancestors() Instance Public methods When localizing a string, it goes through the lookup returned by this method, which is used in ActiveModel::Name#human, ActiveModel::Errors#full_messages and #human_attribute_name.

i18n_scope

i18n_scope() Instance Public methods Returns the i18n_scope for the class. Overwrite if you want custom lookup.

human_attribute_name

human_attribute_name(attribute, options = {}) Instance Public methods Transforms attribute names into a more human format, such as âFirst nameâ instead of âfirst_nameâ. Person.human_attribute_name("first_name") # => "First name" Specify options with additional translating options.

to_xml

to_xml(options = {}, &block) Instance Public methods Returns XML representing the model. Configuration can be passed through options. Without any options, the returned XML string will include all the model's attributes. user = User.find(1) user.to_xml <?xml version="1.0" encoding="UTF-8"?> <user> <id type="integer">1</id> <name>David</name> <age type="integer">16</age> <created-at type="dateTime">2011-01-30T22:29:23