Method: ActiveModel::Model#initialize

Defined in:
activemodel/lib/active_model/model.rb

#initialize(params = {}) ⇒ Object

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

78
79
80
81
82
83
84
# 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