123456789_123456789_123456789_123456789_123456789_

Module: ActiveModel::Model

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Defined in: activemodel/lib/active_model/model.rb

Overview

Active Model Basic Model

Includes the required interface for an object to interact with ::ActionPack, using different ::ActiveModel modules. It includes model name introspections, conversions, translations and validations. Besides that, it allows you to initialize the object with a hash of attributes, pretty much like ::ActiveRecord does.

A minimal implementation could be:

class Person
  include ActiveModel::Model
  attr_accessor :name, :age
end

person = Person.new(name: 'bob', age: '18')
person.name # => "bob"
person.age  # => "18"

Note that, by default, Model implements #persisted? to return false, which is the most common case. You may want to override it in your class to simulate a different scenario:

class Person
  include ActiveModel::Model
  attr_accessor :id, :name

  def persisted?
    self.id == 1
  end
end

person = Person.new(id: 1, name: 'bob')
person.persisted? # => true

Also, if for some reason you need to run code on #initialize, make sure you call super if you want the attributes hash initialization to happen.

class Person
  include ActiveModel::Model
  attr_accessor :id, :name, :omg

  def initialize(attributes={})
    super
    @omg ||= true
  end
end

person = Person.new(id: 1, name: 'bob')
person.omg # => true

For more detailed information on other functionalities available, please refer to the specific modules included in Model (see below).

Validations - Attributes & Methods

Class Method Summary

Naming - self

param_key

Returns string to use for params names.

plural

Returns the plural class name of a record or class.

route_key

Returns string to use while generating route names.

singular

Returns the singular class name of a record or class.

singular_route_key

Returns string to use while generating route names.

uncountable?

Identifies whether the class name of a record or class is uncountable.

::ActiveSupport::Concern - Extended

Instance Attribute Summary

Instance Method Summary

Conversion - Included

#to_key

Returns an ::Array of all key attributes if any is set, regardless if the object is persisted or not.

#to_model

If your object is already designed to implement all of the Active Model you can use the default :to_model implementation, which simply returns self.

#to_param

Returns a string representing the object's key suitable for use in URLs, or nil if #persisted? is false.

#to_partial_path

Returns a string identifying the path associated with the object.

Validations::HelperMethods - self

#validates_absence_of

Validates that the specified attributes are blank (as defined by Object#blank?).

#validates_acceptance_of

Encapsulates the pattern of wanting to validate the acceptance of a terms of service check box (or similar agreement).

#validates_confirmation_of

Encapsulates the pattern of wanting to validate a password or email address field with a confirmation.

#validates_exclusion_of

Validates that the value of the specified attribute is not in a particular enumerable object.

#validates_format_of

Validates whether the value of the specified attribute is of the correct form, going by the regular expression provided.

#validates_inclusion_of

Validates whether the value of the specified attribute is available in a particular enumerable object.

#validates_length_of

Validates that the specified attribute matches the length restrictions supplied.

#validates_numericality_of

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).

#validates_presence_of

Validates that the specified attributes are not blank (as defined by Object#blank?).

#validates_size_of

Validations - Included

#errors

Returns the Errors object that holds all information about attribute error messages.

#invalid?

Performs the opposite of valid?.

#read_attribute_for_validation

Hook method defining how an attribute value should be retrieved.

#valid?

Runs all the specified validations and returns true if no errors were added otherwise false.

#validate
#validates_with

Passes the record off to the class or classes specified and allows them to add errors based on more complex conditions.

DSL Calls

included

[ GitHub ]


63
64
65
66
# File 'activemodel/lib/active_model/model.rb', line 63

included do
  extend ActiveModel::Naming
  extend ActiveModel::Translation
end

Class Attribute Details

._validators (rw)

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations.rb', line 53

class_attribute :_validators, instance_writer: false

._validators?Boolean (rw)

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations.rb', line 53

class_attribute :_validators, instance_writer: false

Instance Attribute Details

#_validators (readonly)

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations.rb', line 53

class_attribute :_validators, instance_writer: false

#_validators?Boolean (readonly)

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations.rb', line 53

class_attribute :_validators, instance_writer: false

#persisted?Boolean (readonly)

Indicates if the model is persisted. Default is false.

class Person
  include ActiveModel::Model
  attr_accessor :id, :name
end

person = Person.new(id: 1, name: 'bob')
person.persisted? # => false
[ GitHub ]

  
# File 'activemodel/lib/active_model/model.rb', line 95

def persisted?
  false
end

#validation_context (rw)

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations.rb', line 49

attr_accessor :validation_context

Instance Method Details

#initialize(params = {})

Initializes a new model with the given params.

class Person
  include ActiveModel::Model
  attr_accessor :name, :age
end

person = Person.new(name: 'bob', age: '18')
person.name # => "bob"
person.age  # => "18"
[ GitHub ]

  
# File 'activemodel/lib/active_model/model.rb', line 78

def initialize(params={})
  params.each do |attr, value|
    self.public_send("#{attr}=", value)
  end if params

  super()
end