123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Association

Relationships & Source Files
Namespace Children
Modules:
Classes:
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Defined in: lib/mongoid/association.rb,
lib/mongoid/association/accessors.rb,
lib/mongoid/association/bindable.rb,
lib/mongoid/association/builders.rb,
lib/mongoid/association/constrainable.rb,
lib/mongoid/association/depending.rb,
lib/mongoid/association/eager.rb,
lib/mongoid/association/eager_loadable.rb,
lib/mongoid/association/macros.rb,
lib/mongoid/association/many.rb,
lib/mongoid/association/marshalable.rb,
lib/mongoid/association/nested.rb,
lib/mongoid/association/one.rb,
lib/mongoid/association/options.rb,
lib/mongoid/association/proxy.rb,
lib/mongoid/association/reflections.rb,
lib/mongoid/association/relatable.rb,
lib/mongoid/association/embedded/batchable.rb,
lib/mongoid/association/embedded/cyclic.rb,
lib/mongoid/association/embedded/eager.rb,
lib/mongoid/association/embedded/embedded_in.rb,
lib/mongoid/association/embedded/embeds_many.rb,
lib/mongoid/association/embedded/embeds_one.rb,
lib/mongoid/association/embedded/embedded_in/binding.rb,
lib/mongoid/association/embedded/embedded_in/buildable.rb,
lib/mongoid/association/embedded/embedded_in/proxy.rb,
lib/mongoid/association/embedded/embeds_many/binding.rb,
lib/mongoid/association/embedded/embeds_many/buildable.rb,
lib/mongoid/association/embedded/embeds_many/proxy.rb,
lib/mongoid/association/embedded/embeds_one/binding.rb,
lib/mongoid/association/embedded/embeds_one/buildable.rb,
lib/mongoid/association/embedded/embeds_one/proxy.rb,
lib/mongoid/association/nested/many.rb,
lib/mongoid/association/nested/nested_buildable.rb,
lib/mongoid/association/nested/one.rb,
lib/mongoid/association/referenced/auto_save.rb,
lib/mongoid/association/referenced/belongs_to.rb,
lib/mongoid/association/referenced/counter_cache.rb,
lib/mongoid/association/referenced/has_and_belongs_to_many.rb,
lib/mongoid/association/referenced/has_many.rb,
lib/mongoid/association/referenced/has_one.rb,
lib/mongoid/association/referenced/syncable.rb,
lib/mongoid/association/referenced/with_polymorphic_criteria.rb,
lib/mongoid/association/referenced/belongs_to/binding.rb,
lib/mongoid/association/referenced/belongs_to/buildable.rb,
lib/mongoid/association/referenced/belongs_to/eager.rb,
lib/mongoid/association/referenced/belongs_to/proxy.rb,
lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb,
lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.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_many/binding.rb,
lib/mongoid/association/referenced/has_many/buildable.rb,
lib/mongoid/association/referenced/has_many/eager.rb,
lib/mongoid/association/referenced/has_many/enumerable.rb,
lib/mongoid/association/referenced/has_many/proxy.rb,
lib/mongoid/association/referenced/has_one/binding.rb,
lib/mongoid/association/referenced/has_one/buildable.rb,
lib/mongoid/association/referenced/has_one/eager.rb,
lib/mongoid/association/referenced/has_one/proxy.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.

Constant Summary

Depending - Included

STRATEGIES

Depending - Attributes & Methods

Macros - Attributes & Methods

Association::Embedded::Cyclic - Attributes & Methods

Class Method Summary

Association::Reflections::ClassMethods - Extended

reflect_on_all_associations

Returns all association metadata for the supplied macros.

reflect_on_association

Returns the association metadata for the supplied name.

Association::Macros::ClassMethods - Extended

belongs_to

Adds a referenced association from the child Document to a Document in another database or collection.

embedded_in

Adds the association back to the parent document.

embeds_many

Adds the association from a parent document to its children.

embeds_one

Adds the association from a parent document to its child.

has_and_belongs_to_many

Adds a referenced many-to-many association between many of this Document and many of another Document.

has_many

Adds a referenced association from a parent Document to many Documents in another database or collection.

has_one

Adds a referenced association from the child Document to a Document in another database or collection.

define_association!

Association::Referenced::Syncable::ClassMethods - Extended

_synced

::Set up the syncing of many to many foreign keys.

synced_destroy

::Set up the sync of inverse keys that needs to happen on a destroy.

synced_save

::Set up the sync of inverse keys that needs to happen on a save.

Association::Referenced::CounterCache::ClassMethods - Extended

decrement_counter

Decrement the counter name from the entries that match the id by one.

increment_counter

Increment the counter name from the entries that match the id by one.

reset_counters

Reset the given counter using the .count() query from the db.

update_counters

Update the given counters by the value factor.

Association::Embedded::Cyclic::ClassMethods - Extended

recursively_embeds_many

Create a cyclic embedded association that creates a tree hierarchy for the document and many embedded child documents.

recursively_embeds_one

Create a cyclic embedded association that creates a single self referencing relationship for a parent and a single child.

cyclic_child_name

Determines the child name given the class.

cyclic_parent_name

Determines the parent name given the class.

Instance Attribute Summary

Accessors - Included

#without_autobuild?

Is the current code executing without autobuild functionality?

Association::Referenced::AutoSave - Included

#autosaved?

Used to prevent infinite loops in associated autosaves.

Instance Method Summary

Reflections - Included

#reflect_on_all_association

Returns all association metadata for the supplied macros.

#reflect_on_association

Returns the association metadata for the supplied name.

Macros - Included

#associations

This is convenience for libraries still on the old API.

Builders - Included

#parse_args

Parse out the attributes and the options from the args passed to a build_ or create_ methods.

Depending - Included

Accessors - Included

#__build__

Builds the related document and creates the association unless the document is nil, then sets the association on this document.

#create_relation

Create an association from an object and association metadata.

#reset_relation_criteria

Resets the criteria inside the association proxy.

#set_relation

::Set the supplied association to an instance variable on the class with the provided name.

#_mongoid_filter_selected_fields

Returns a subset of __selected_fields attribute applicable to the (embedded) association with the given key, or nil if no projection is to be performed.

#get_relation

Get the association.

#needs_no_database_query?,
#parse_args

Parse out the attributes and the options from the args passed to a build_ or create_ methods.

#without_autobuild

Yield to the block with autobuild functionality turned off.

Association::Referenced::Syncable - Included

#_syncable?

Is the document able to be synced on the inverse side? This is only if the key has changed and the association bindings have not been run.

#_synced

Get the synced foreign keys.

#_synced?

Has the document been synced for the foreign key?

#remove_inverse_keys

Update the inverse keys on destroy.

#update_inverse_keys

Update the inverse keys for the association.

Association::Referenced::CounterCache - Included

#reset_counters

Reset the given counter using the .count() query from the db.

Association::Referenced::AutoSave - Included

#__autosaving__

Begin the associated autosave.

#changed_for_autosave?

Check if there is changes for auto-saving.

DSL Calls

included

[ GitHub ]


53
54
55
56
# File 'lib/mongoid/association.rb', line 53

included do
  class_attribute :polymorphic
  self.polymorphic = false
end

Class Attribute Details

.aliased_associations (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/macros.rb', line 35

class_attribute :aliased_associations

.aliased_associations?Boolean (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/macros.rb', line 35

class_attribute :aliased_associations

.cyclic (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/embedded/cyclic.rb', line 14

class_attribute :cyclic

.cyclic?Boolean (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/embedded/cyclic.rb', line 14

class_attribute :cyclic

.dependents (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/depending.rb', line 13

class_attribute :dependents

.dependents?Boolean (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/depending.rb', line 13

class_attribute :dependents

.dependents_owner (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/depending.rb', line 16

class_attribute :dependents_owner

.dependents_owner?Boolean (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/depending.rb', line 16

class_attribute :dependents_owner

.embedded (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/macros.rb', line 11

class_attribute :embedded, instance_reader: false

.embedded?Boolean (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/macros.rb', line 11

class_attribute :embedded, instance_reader: false

.embedded_relations (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/macros.rb', line 12

class_attribute :embedded_relations

.embedded_relations?Boolean (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/macros.rb', line 12

class_attribute :embedded_relations

.relations (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/macros.rb', line 13

class_attribute :relations

.relations?Boolean (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/macros.rb', line 13

class_attribute :relations

.stored_as_associations (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/macros.rb', line 39

class_attribute :stored_as_associations

.stored_as_associations?Boolean (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/macros.rb', line 39

class_attribute :stored_as_associations

Instance Attribute Details

#_association (rw)

[ GitHub ]

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

attr_accessor :_association

#aliased_associations (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/macros.rb', line 35

class_attribute :aliased_associations

#aliased_associations?Boolean (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/macros.rb', line 35

class_attribute :aliased_associations

#cyclic (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/embedded/cyclic.rb', line 14

class_attribute :cyclic

#cyclic?Boolean (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/embedded/cyclic.rb', line 14

class_attribute :cyclic

#dependents (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/depending.rb', line 13

class_attribute :dependents

#dependents?Boolean (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/depending.rb', line 13

class_attribute :dependents

#dependents_owner (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/depending.rb', line 16

class_attribute :dependents_owner

#dependents_owner?Boolean (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/depending.rb', line 16

class_attribute :dependents_owner

#embedded?true | false (rw)

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.

[ GitHub ]

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

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

#embedded_many?true | false (readonly)

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.

[ GitHub ]

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

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

#embedded_one?true | false (readonly)

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.

[ GitHub ]

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

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

#embedded_relations (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/macros.rb', line 12

class_attribute :embedded_relations

#embedded_relations?Boolean (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/macros.rb', line 12

class_attribute :embedded_relations

#referenced_many?true | false (readonly)

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.

[ GitHub ]

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

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

#referenced_one?true | false (readonly)

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.

[ GitHub ]

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

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

#relations (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/macros.rb', line 13

class_attribute :relations

#relations?Boolean (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/macros.rb', line 13

class_attribute :relations

#stored_as_associations (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/macros.rb', line 39

class_attribute :stored_as_associations

#stored_as_associations?Boolean (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/macros.rb', line 39

class_attribute :stored_as_associations

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:

[ GitHub ]

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

def association_name
  raise Errors::NoMetadata.new(self.class.name) unless _association
  _association.name
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.

[ GitHub ]

  
# 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