Method: ActiveRecord::Core#initialize

Defined in:
lib/active_record/core.rb

#initialize(attributes = nil) {|_self| ... } ⇒ Object

New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes but not yet saved (pass a hash with key names matching the associated table column names). In both instances, valid attribute keys are determined by the column names of the associated table – hence you can’t have attributes that aren’t part of the table columns.

Example:

# Instantiates a single new object
User.new(first_name: 'Jamie')

Yields:

  • (_self)

Yield Parameters:


330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/active_record/core.rb', line 330

def initialize(attributes = nil)
  self.class.define_attribute_methods
  @attributes = self.class._default_attributes.deep_dup

  init_internals
  initialize_internals_callback

  assign_attributes(attributes) if attributes

  yield self if block_given?
  _run_initialize_callbacks
end