Method: Mongoid::Relations::Builders::ClassMethods#creator

Defined in:
lib/mongoid/relations/builders.rb

#creator(name, metadata) ⇒ Class

Defines a creator method for an embeds_one relation. This is defined as #create_name. After the object is built it will immediately save.

Examples:

Person.creator("name")

Parameters:

  • name (String, Symbol)

    The name of the relation.

Returns:

  • (Class)

    The class being set up.

Since:

  • 2.0.0.rc.1



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mongoid/relations/builders.rb', line 90

def creator(name, )
  re_define_method("create_#{name}") do |*args|
    attributes, options = parse_args(*args)
    document = Factory.build(.klass, attributes, options)
    doc = send("#{name}=", document)
    doc.save
    save if new_record? && .stores_foreign_key?
    doc
  end
  self
end