validates_exclusion_of

validates_exclusion_of(*attr_names) Instance Public methods Validates that the value of the specified attribute is not in a particular enumerable object. class Person < ActiveRecord::Base validates_exclusion_of :username, in: %w( admin superuser ), message: "You don't belong here" validates_exclusion_of :age, in: 30..60, message: 'This site is only for under 30 and over 60' validates_exclusion_of :format, in: %w( mov avi ), message: "extension %{value} is not allowed" va

validates_format_of

validates_format_of(*attr_names) Instance Public methods Validates whether the value of the specified attribute is of the correct form, going by the regular expression provided.You can require that the attribute matches the regular expression: class Person < ActiveRecord::Base validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: :create end Alternatively, you can require that the specified attribute does not match the regular expression: class

validates_inclusion_of

validates_inclusion_of(*attr_names) Instance Public methods Validates whether the value of the specified attribute is available in a particular enumerable object. class Person < ActiveRecord::Base validates_inclusion_of :gender, in: %w( m f ) validates_inclusion_of :age, in: 0..99 validates_inclusion_of :format, in: %w( jpg gif png ), message: "extension %{value} is not included in the list" validates_inclusion_of :states, in: ->(person) { STATES[person.country] } v

validates_length_of

validates_length_of(*attr_names) Instance Public methods Validates that the specified attribute matches the length restrictions supplied. Only one option can be used at a time: class Person < ActiveRecord::Base validates_length_of :first_name, maximum: 30 validates_length_of :last_name, maximum: 30, message: "less than 30 if you don't mind" validates_length_of :fax, in: 7..32, allow_nil: true validates_length_of :phone, in: 7..32, allow_blank: true validates_length_of

validates_numericality_of

validates_numericality_of(*attr_names) Instance Public methods Validates whether the value of the specified attribute is numeric by trying to convert it to a float with Kernel.Float (if only_integer is false) or applying it to the regular expression /\A[+\-]?\d+\Z/ (if only_integer is set to true). class Person < ActiveRecord::Base validates_numericality_of :value, on: :create end Configuration options: :message - A custom error message (default is: âis not a numberâ). :on

validates_presence_of

validates_presence_of(*attr_names) Instance Public methods Validates that the specified attributes are not blank (as defined by Object#blank?). Happens by default on save. class Person < ActiveRecord::Base validates_presence_of :first_name end The first_name attribute must be in the object and it cannot be blank. If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, in: [tru

validates_size_of

validates_size_of(*attr_names) Instance Public methods Alias for: validates_length_of

errors

errors() Instance Public methods Returns the Errors object that holds all information about attribute error messages. class Person include ActiveModel::Validations attr_accessor :name validates_presence_of :name end person = Person.new person.valid? # => false person.errors # => #<ActiveModel::Errors:0x007fe603816640 @messages={name:["can't be blank"]}>

invalid?

invalid?(context = nil) Instance Public methods Performs the opposite of valid?. Returns true if errors were added, false otherwise. class Person include ActiveModel::Validations attr_accessor :name validates_presence_of :name end person = Person.new person.name = '' person.invalid? # => true person.name = 'david' person.invalid? # => false Context can optionally be supplied to define which callbacks to test against (the context is defined on the validations using :o

valid?

valid?(context = nil) Instance Public methods Runs all the specified validations and returns true if no errors were added otherwise false. class Person include ActiveModel::Validations attr_accessor :name validates_presence_of :name end person = Person.new person.name = '' person.valid? # => false person.name = 'david' person.valid? # => true Context can optionally be supplied to define which callbacks to test against (the context is defined on the validations using