instance method
new
Ruby on Rails 5.2.8.1
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 56
def new(attributes = nil, &block)
scoping { klass.new(values_for_create(attributes), &block) }
end
Defined in activerecord/lib/active_record/relation.rb line 56
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Relation