Ruby on Rails ActiveModel


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

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