instance method
new
Ruby on Rails 8.1.2
Since v3.0.20Signature
new(attributes = nil, &block)
Initializes new record from relation while maintaining the current scope.
Expects arguments in the same format as ActiveRecord::Base.new.
users = User.where(name: 'DHH')
user = users.new # => #<User id: nil, name: "DHH", created_at: nil, updated_at: nil>
You can also pass a block to new with the new record as argument:
user = users.new { |user| user.name = 'Oscar' }
user.name # => Oscar
Parameters
-
attributesopt = nil -
blockblock
Source
# File activerecord/lib/active_record/relation.rb, line 125
def new(attributes = nil, &block)
if attributes.is_a?(Array)
attributes.collect { |attr| new(attr, &block) }
else
block = current_scope_restoring_block(&block)
scoping { _new(attributes, &block) }
end
end
Defined in activerecord/lib/active_record/relation.rb line 125
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Relation