class method self.new

Ruby on Rails 4.0.13

Since v4.0.13

Available in: v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

self.new(attributes = nil, options = {})

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

Parameters

attributes opt = nil
options opt = {}
Source
# File activerecord/lib/active_record/core.rb, line 170
    def initialize(attributes = nil, options = {})
      defaults = self.class.column_defaults.dup
      defaults.each { |k, v| defaults[k] = v.dup if v.duplicable? }

      @attributes   = self.class.initialize_attributes(defaults)
      @column_types_override = nil
      @column_types = self.class.column_types

      init_internals
      init_changed_attributes
      ensure_proper_type
      populate_with_current_scope_attributes

      # +options+ argument is only needed to make protected_attributes gem easier to hook.
      # Remove it when we drop support to this gem.
      init_attributes(attributes, options) if attributes

      yield self if block_given?
      run_callbacks :initialize unless _initialize_callbacks.empty?
    end

Defined in activerecord/lib/active_record/core.rb line 170 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::Core

Type at least 2 characters to search.

↑↓ navigate · open · esc close