Method: Dynamoid::Persistence::ClassMethods#create
- Defined in:
- lib/dynamoid/persistence.rb
#create(attrs = {}, &block) ⇒ Dynamoid::Document
Create a model.
Initializes a new model and immediately saves it into DynamoDB.
User.create(first_name: 'Mark', last_name: 'Tyler')
Accepts both Hash and Array of Hashes and can create several models.
User.create([{ first_name: 'Alice' }, { first_name: 'Bob' }])
Instantiates a model and pass it into an optional block to set other attributes.
User.create(first_name: 'Mark') do |u|
u.age = 21
end
Validates model and runs callbacks.
Raises Dynamoid::Errors::MissingRangeKey
if a sort key is required but not specified or has value nil
.
195 196 197 198 199 200 201 |
# File 'lib/dynamoid/persistence.rb', line 195 def create(attrs = {}, &block) if attrs.is_a?(Array) attrs.map { |attr| create(attr, &block) } else build(attrs, &block).tap(&:save) end end |