Module: Mongoid::Association

Extended by:
ActiveSupport::Concern
Includes:
Accessors, Builders, Depending, Embedded::Cyclic, Macros, Referenced::AutoSave, Referenced::CounterCache, Referenced::Syncable, Reflections
Included in:
Composable
Defined in:
lib/mongoid/association.rb,
lib/mongoid/association/one.rb,
lib/mongoid/association/many.rb,
lib/mongoid/association/eager.rb,
lib/mongoid/association/proxy.rb,
lib/mongoid/association/macros.rb,
lib/mongoid/association/nested.rb,
lib/mongoid/association/options.rb,
lib/mongoid/association/bindable.rb,
lib/mongoid/association/builders.rb,
lib/mongoid/association/accessors.rb,
lib/mongoid/association/depending.rb,
lib/mongoid/association/relatable.rb,
lib/mongoid/association/nested/one.rb,
lib/mongoid/association/marshalable.rb,
lib/mongoid/association/nested/many.rb,
lib/mongoid/association/reflections.rb,
lib/mongoid/association/constrainable.rb,
lib/mongoid/association/eager_loadable.rb,
lib/mongoid/association/embedded/eager.rb,
lib/mongoid/association/embedded/cyclic.rb,
lib/mongoid/association/embedded/batchable.rb,
lib/mongoid/association/referenced/has_one.rb,
lib/mongoid/association/embedded/embeds_one.rb,
lib/mongoid/association/referenced/has_many.rb,
lib/mongoid/association/referenced/syncable.rb,
lib/mongoid/association/embedded/embedded_in.rb,
lib/mongoid/association/embedded/embeds_many.rb,
lib/mongoid/association/referenced/auto_save.rb,
lib/mongoid/association/referenced/belongs_to.rb,
lib/mongoid/association/nested/nested_buildable.rb,
lib/mongoid/association/referenced/counter_cache.rb,
lib/mongoid/association/referenced/has_one/eager.rb,
lib/mongoid/association/referenced/has_one/proxy.rb,
lib/mongoid/association/embedded/embeds_one/proxy.rb,
lib/mongoid/association/referenced/has_many/eager.rb,
lib/mongoid/association/referenced/has_many/proxy.rb,
lib/mongoid/association/embedded/embedded_in/proxy.rb,
lib/mongoid/association/embedded/embeds_many/proxy.rb,
lib/mongoid/association/referenced/has_one/binding.rb,
lib/mongoid/association/embedded/embeds_one/binding.rb,
lib/mongoid/association/referenced/belongs_to/eager.rb,
lib/mongoid/association/referenced/belongs_to/proxy.rb,
lib/mongoid/association/referenced/has_many/binding.rb,
lib/mongoid/association/embedded/embedded_in/binding.rb,
lib/mongoid/association/embedded/embeds_many/binding.rb,
lib/mongoid/association/referenced/has_one/buildable.rb,
lib/mongoid/association/embedded/embeds_one/buildable.rb,
lib/mongoid/association/referenced/belongs_to/binding.rb,
lib/mongoid/association/referenced/has_many/buildable.rb,
lib/mongoid/association/embedded/embedded_in/buildable.rb,
lib/mongoid/association/embedded/embeds_many/buildable.rb,
lib/mongoid/association/referenced/has_many/enumerable.rb,
lib/mongoid/association/referenced/belongs_to/buildable.rb,
lib/mongoid/association/referenced/has_and_belongs_to_many.rb,
lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb,
lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb,
lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb,
lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb

Overview

Mixin module which adds association behavior to a Mongoid document. Adds methods such as #embedded? which indicate a document’s relative association state.

Defined Under Namespace

Modules: Accessors, Bindable, Builders, Constrainable, Depending, EagerLoadable, Embedded, Macros, Marshalable, Nested, Options, Referenced, Reflections, Relatable Classes: Eager, Many, One, Proxy

Constant Summary collapse

MACRO_MAPPING =

Map the macros to their corresponding Association classes.

Returns:

  • (Hash)

    The mapping from macros to their Association class.

{
    embeds_one: Association::Embedded::EmbedsOne,
    embeds_many: Association::Embedded::EmbedsMany,
    embedded_in: Association::Embedded::EmbeddedIn,
    has_one: Association::Referenced::HasOne,
    has_many: Association::Referenced::HasMany,
    has_and_belongs_to_many: Association::Referenced::HasAndBelongsToMany,
    belongs_to: Association::Referenced::BelongsTo,
}.freeze

Constants included from Depending

Depending::STRATEGIES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Reflections

#reflect_on_all_association, #reflect_on_association

Methods included from Macros

#associations

Methods included from Depending

#apply_destroy_dependencies!, define_dependency!, validate!

Methods included from Accessors

#__build__, #create_relation, #reset_relation_criteria, #set_relation

Methods included from Referenced::Syncable

#_syncable?, #_synced, #_synced?, #remove_inverse_keys, #update_inverse_keys

Methods included from Referenced::CounterCache

define_callbacks!, #reset_counters

Methods included from Referenced::AutoSave

#__autosaving__, #autosaved?, #changed_for_autosave?, define_autosave!

Instance Attribute Details

#_associationObject

Returns the value of attribute _association.



51
52
53
# File 'lib/mongoid/association.rb', line 51

def _association
  @_association
end

Instance Method Details

#association_nameSymbol

Get the association name for this document. If no association was defined

an error will be raised.

Examples:

Get the association name.

document.association_name

Returns:

  • (Symbol)

    The association name.

Raises:



98
99
100
101
# File 'lib/mongoid/association.rb', line 98

def association_name
  raise Errors::NoMetadata.new(self.class.name) unless _association
  _association.name
end

#embedded?true | false

Determine if the document itself is embedded in another document via the proper channels. (If it has a parent document.)

Examples:

Is the document embedded?

address.embedded?

Returns:

  • (true | false)

    True if the document has a parent document.



65
66
67
# File 'lib/mongoid/association.rb', line 65

def embedded?
  @embedded ||= (cyclic ? _parent.present? : self.class.embedded?)
end

#embedded_many?true | false

Determine if the document is part of an embeds_many association.

Examples:

Is the document in an embeds many?

address.embedded_many?

Returns:

  • (true | false)

    True if in an embeds many.



75
76
77
# File 'lib/mongoid/association.rb', line 75

def embedded_many?
  _association && _association.is_a?(Association::Embedded::EmbedsMany)
end

#embedded_one?true | false

Determine if the document is part of an embeds_one association.

Examples:

Is the document in an embeds one?

address.embedded_one?

Returns:

  • (true | false)

    True if in an embeds one.



85
86
87
# File 'lib/mongoid/association.rb', line 85

def embedded_one?
  _association && _association.is_a?(Association::Embedded::EmbedsOne)
end

#referenced_many?true | false

Determine if the document is part of an references_many association.

Examples:

Is the document in a references many?

post.referenced_many?

Returns:

  • (true | false)

    True if in a references many.



109
110
111
# File 'lib/mongoid/association.rb', line 109

def referenced_many?
  _association && _association.is_a?(Association::Referenced::HasMany)
end

#referenced_one?true | false

Determine if the document is part of an references_one association.

Examples:

Is the document in a references one?

address.referenced_one?

Returns:

  • (true | false)

    True if in a references one.



119
120
121
# File 'lib/mongoid/association.rb', line 119

def referenced_one?
  _association && _association.is_a?(Association::Referenced::HasOne)
end

#reload_relationsHash

Convenience method for iterating through the loaded associations and reloading them.

Examples:

Reload the associations.

document.reload_relations

Returns:

  • (Hash)

    The association metadata.



130
131
132
133
134
135
136
137
138
# File 'lib/mongoid/association.rb', line 130

def reload_relations
  relations.each_pair do |name, meta|
    if instance_variable_defined?("@_#{name}")
      if _parent.nil? || instance_variable_get("@_#{name}") != _parent
        remove_instance_variable("@_#{name}")
      end
    end
  end
end