Module: Mongoid::Relations::Macros::ClassMethods

Defined in:
lib/mongoid/core_ext/relations/macros.rb

Instance Method Summary collapse

Instance Method Details

#embedded_in(name, options = {}, &block) ⇒ Object

Adds the relation back to the parent document. This macro is necessary to set the references from the child back to the parent document. If a child does not define this relation calling persistence methods on the child object will cause a save to fail.

Examples:

Define the relation.


class Person
  include Mongoid::Document
  embeds_many :addresses
end

class Address
  include Mongoid::Document
  embedded_in :person
end

Parameters:

  • name (Symbol)

    The name of the relation.

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

    The relation options.

  • block (Proc)

    Optional block for defining extensions.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mongoid/core_ext/relations/macros.rb', line 25

def embedded_in(name, options = {}, &block)
  if ancestors.include?(Mongoid::Versioning)
    raise Errors::VersioningNotOnRoot.new(self)
  end
  meta = characterize(name, Embedded::In, options, &block)
  self.embedded = true
  relate(name, meta)
  builder(name, meta).creator(name, meta)
  add_counter_cache_callbacks(meta) if meta.counter_cached?
  meta
end