instance method
create
Ruby on Rails 7.2.3
Since v3.0.20Signature
create(attributes = nil, &block)
Tries to create a new record with the same scoped attributes defined in the relation. Returns the initialized object if validation fails.
Expects arguments in the same format as ActiveRecord::Base.create.
Examples
users = User.where(name: 'Oscar')
users.create # => #<User id: 3, name: "Oscar", ...>
users.create(name: 'fxn')
users.create # => #<User id: 4, name: "fxn", ...>
users.create { |user| user.name = 'tenderlove' }
# => #<User id: 5, name: "tenderlove", ...>
users.create(name: nil) # validation on name
# => #<User id: nil, name: nil, ...>
Parameters
-
attributesopt = nil -
blockblock
Source
# File activerecord/lib/active_record/relation.rb, line 147
def create(attributes = nil, &block)
if attributes.is_a?(Array)
attributes.collect { |attr| create(attr, &block) }
else
block = current_scope_restoring_block(&block)
scoping { _create(attributes, &block) }
end
end
Defined in activerecord/lib/active_record/relation.rb line 147
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Relation