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/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/cyclic.rb,
lib/mongoid/association/referenced/eager.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_one/nested_builder.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

Defined Under Namespace

Modules: Accessors, Bindable, Builders, Constrainable, Depending, EagerLoadable, Embedded, Macros, Marshalable, Nested, Options, Referenced, Reflections, Relatable Classes: 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.

Since:

  • 7.0

{
    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::RESTRICT_ERROR_MSG, 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_delete_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



46
47
48
# File 'lib/mongoid/association.rb', line 46

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:

Since:

  • 3.0.0



101
102
103
104
# File 'lib/mongoid/association.rb', line 101

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.

Since:

  • 2.0.0.rc.1



62
63
64
# File 'lib/mongoid/association.rb', line 62

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

#embedded_many?true, false

Determine if the document is part of an embeds_many relation.

Examples:

Is the document in an embeds many?

address.embedded_many?

Returns:

  • (true, false)

    True if in an embeds many.

Since:

  • 2.0.0.rc.1



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

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 relation.

Examples:

Is the document in an embeds one?

address.embedded_one?

Returns:

  • (true, false)

    True if in an embeds one.

Since:

  • 2.0.0.rc.1



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

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 relation.

Examples:

Is the document in a references many?

post.referenced_many?

Returns:

  • (true, false)

    True if in a references many.

Since:

  • 2.0.0.rc.1



114
115
116
# File 'lib/mongoid/association.rb', line 114

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 relation.

Examples:

Is the document in a references one?

address.referenced_one?

Returns:

  • (true, false)

    True if in a references one.

Since:

  • 2.0.0.rc.1



126
127
128
# File 'lib/mongoid/association.rb', line 126

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

#reload_relationsHash

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

Examples:

Reload the relations.

document.reload_relations

Returns:

  • (Hash)

    The association metadata.

Since:

  • 2.1.6



139
140
141
142
143
144
145
146
147
# File 'lib/mongoid/association.rb', line 139

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