class method
self.new
Ruby on Rails 4.2.9
Since v4.0.13Signature
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
-
attributesopt = nil -
optionsopt = {}
Source
# File activerecord/lib/active_record/core.rb, line 272
def initialize(attributes = nil, options = {})
@attributes = self.class._default_attributes.dup
self.class.define_attribute_methods
init_internals
initialize_internals_callback
# +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_initialize_callbacks
end
Defined in activerecord/lib/active_record/core.rb line 272
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Core