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:
build/mongoid-7.3/lib/mongoid/association.rb,
build/mongoid-7.3/lib/mongoid/association/one.rb,
build/mongoid-7.3/lib/mongoid/association/many.rb,
build/mongoid-7.3/lib/mongoid/association/proxy.rb,
build/mongoid-7.3/lib/mongoid/association/macros.rb,
build/mongoid-7.3/lib/mongoid/association/nested.rb,
build/mongoid-7.3/lib/mongoid/association/options.rb,
build/mongoid-7.3/lib/mongoid/association/bindable.rb,
build/mongoid-7.3/lib/mongoid/association/builders.rb,
build/mongoid-7.3/lib/mongoid/association/accessors.rb,
build/mongoid-7.3/lib/mongoid/association/depending.rb,
build/mongoid-7.3/lib/mongoid/association/relatable.rb,
build/mongoid-7.3/lib/mongoid/association/nested/one.rb,
build/mongoid-7.3/lib/mongoid/association/marshalable.rb,
build/mongoid-7.3/lib/mongoid/association/nested/many.rb,
build/mongoid-7.3/lib/mongoid/association/reflections.rb,
build/mongoid-7.3/lib/mongoid/association/constrainable.rb,
build/mongoid-7.3/lib/mongoid/association/eager_loadable.rb,
build/mongoid-7.3/lib/mongoid/association/embedded/cyclic.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/eager.rb,
build/mongoid-7.3/lib/mongoid/association/embedded/batchable.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_one.rb,
build/mongoid-7.3/lib/mongoid/association/embedded/embeds_one.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_many.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/syncable.rb,
build/mongoid-7.3/lib/mongoid/association/embedded/embedded_in.rb,
build/mongoid-7.3/lib/mongoid/association/embedded/embeds_many.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/auto_save.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/belongs_to.rb,
build/mongoid-7.3/lib/mongoid/association/nested/nested_buildable.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/counter_cache.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_one/eager.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_one/proxy.rb,
build/mongoid-7.3/lib/mongoid/association/embedded/embeds_one/proxy.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_many/eager.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_many/proxy.rb,
build/mongoid-7.3/lib/mongoid/association/embedded/embedded_in/proxy.rb,
build/mongoid-7.3/lib/mongoid/association/embedded/embeds_many/proxy.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_one/binding.rb,
build/mongoid-7.3/lib/mongoid/association/embedded/embeds_one/binding.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/belongs_to/eager.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/belongs_to/proxy.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_many/binding.rb,
build/mongoid-7.3/lib/mongoid/association/embedded/embedded_in/binding.rb,
build/mongoid-7.3/lib/mongoid/association/embedded/embeds_many/binding.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_one/buildable.rb,
build/mongoid-7.3/lib/mongoid/association/embedded/embeds_one/buildable.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/belongs_to/binding.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_many/buildable.rb,
build/mongoid-7.3/lib/mongoid/association/embedded/embedded_in/buildable.rb,
build/mongoid-7.3/lib/mongoid/association/embedded/embeds_many/buildable.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_many/enumerable.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/belongs_to/buildable.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_one/nested_builder.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_and_belongs_to_many.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb,
build/mongoid-7.3/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::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.



49
50
51
# File 'build/mongoid-7.3/lib/mongoid/association.rb', line 49

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



104
105
106
107
# File 'build/mongoid-7.3/lib/mongoid/association.rb', line 104

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



65
66
67
# File 'build/mongoid-7.3/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.

Since:

  • 2.0.0.rc.1



77
78
79
# File 'build/mongoid-7.3/lib/mongoid/association.rb', line 77

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.

Since:

  • 2.0.0.rc.1



89
90
91
# File 'build/mongoid-7.3/lib/mongoid/association.rb', line 89

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.

Since:

  • 2.0.0.rc.1



117
118
119
# File 'build/mongoid-7.3/lib/mongoid/association.rb', line 117

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.

Since:

  • 2.0.0.rc.1



129
130
131
# File 'build/mongoid-7.3/lib/mongoid/association.rb', line 129

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.

Since:

  • 2.1.6



142
143
144
145
146
147
148
149
150
# File 'build/mongoid-7.3/lib/mongoid/association.rb', line 142

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