Ruby on Rails

ActiveModel

Remarks#

ActiveModel was created to extract the model behavior of ActiveRecord into a separate concern. This allows us to use ActiveModel behavior in any object, not just ActiveRecord models.

ActiveRecord objects include all of this behavior by default.

Using ActiveModel::Validations

You can validate any object, even plain ruby.

class User
  include ActiveModel::Validations

  attr_reader :name, :age

  def initialize(name, age)
    @name = name
    @age  = age
  end

  validates :name, presence: true
  validates :age, numericality: { only_integer: true, greater_than: 12 }
end
User.new('John Smith', 28).valid? #=> true
User.new('Jane Smith', 11).valid? #=> false
User.new(nil, 30).valid?          #=> false

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow