Method: Dynamoid::Document::ClassMethods#build

Defined in:
lib/dynamoid/document.rb

#build(attrs = {}, &block) ⇒ Dynamoid::Document

Initialize a new object.

User.build(name: 'A')

Initialize an object and pass it into a block to set other attributes.

User.build(name: 'A') do |u|
  u.age = 21
end

The only difference between build and new methods is that build supports STI (Single table inheritance) and looks at the inheritance field. So it can build a model of actual class. For instance:

class Employee
  include Dynamoid::Document

  field :type
  field :name
end

class Manager < Employee
end

Employee.build(name: 'Alice', type: 'Manager') # => #<Manager:0x00007f945756e3f0 ...>

Parameters:

  • attrs (Hash) (defaults to: {})

    Attributes with which to create the document

  • block (Proc)

    Block to process a document after initialization

Returns:

Since:

  • 0.2.0



116
117
118
# File 'lib/dynamoid/document.rb', line 116

def build(attrs = {}, &block)
  choose_right_class(attrs).new(attrs, &block)
end