Module: Mongoid::Association
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
-
MACRO_MAPPING =
# File 'lib/mongoid/association.rb', line 47
Map the macros to their corresponding
Associationclasses.{ 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 -
PRELOADED_PREFIX =
# File 'lib/mongoid/association.rb', line 42
Prefixes the field a $lookup writes an association's preloaded documents into. They travel apart from the document's own fields, so the hydration can keep them out of the attributes instead of leaving them on the document, where a later save would persist them.
'__mongoid_eager_loaded_' -
THROUGH_MACRO_MAPPING =
# File 'lib/mongoid/association.rb', line 59
Internal mapping used when
:throughoption is present. Not exposed as callable macros.{ has_one: Association::Referenced::HasOneThrough, has_many: Association::Referenced::HasManyThrough }.freeze
Depending - Included
Depending - Attributes & Methods
Macros - Attributes & Methods
- .aliased_associations rw
- #aliased_associations rw
- .aliased_associations? ⇒ Boolean rw
- #aliased_associations? ⇒ Boolean rw
- .embedded rw
- .embedded? ⇒ Boolean rw
- .embedded_relations rw
- #embedded_relations rw
- .embedded_relations? ⇒ Boolean rw
- #embedded_relations? ⇒ Boolean rw
- .relations rw
- #relations rw
- .relations? ⇒ Boolean rw
- #relations? ⇒ Boolean rw
- .stored_as_associations rw
- #stored_as_associations rw
- .stored_as_associations? ⇒ Boolean rw
- #stored_as_associations? ⇒ Boolean rw
Association::Embedded::Cyclic - Attributes & Methods
- .cyclic rw
- #cyclic rw
- .cyclic? ⇒ Boolean rw
- #cyclic? ⇒ Boolean rw
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 | |
| 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 | |
| has_many | Adds a referenced association from a parent |
| has_one | |
| define_association! | |
Association::Referenced::Syncable::ClassMethods - Extended
| _synced |
|
| synced_destroy |
|
| synced_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
- #_association rw
-
#embedded? ⇒ true | false
rw
Determine if the document itself is embedded in another document via the proper channels.
-
#embedded_many? ⇒ true | false
readonly
Determine if the document is part of an embeds_many association.
-
#embedded_one? ⇒ true | false
readonly
Determine if the document is part of an embeds_one association.
-
#referenced_many? ⇒ true | false
readonly
Determine if the document is part of an references_many association.
-
#referenced_one? ⇒ true | false
readonly
Determine if the document is part of an references_one association.
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
-
#association_name ⇒ Symbol
Get the association name for this document.
-
#reload_relations ⇒ Hash
Convenience method for iterating through the loaded associations and reloading them.
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 | Parses the arguments passed to the build and create methods. |
Depending - Included
| #apply_destroy_dependencies! | Perform all cascading deletes, destroys, or nullifies. |
| #_dependent_delete_all!, #_dependent_destroy!, #_dependent_nullify!, #_dependent_restrict_with_error!, #_dependent_restrict_with_exception! | |
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. |
| #load_preloaded_associations | Loads the documents the query preloaded for this one, kept apart from the attributes so they are not part of the document. |
| #reset_relation_criteria | Resets the criteria inside the association proxy. |
| #set_relation |
|
| #_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 are changes for auto-saving. |
| #autosave_children_changed? | Returns true if any in-memory referenced child with autosave: true needs saving. |
| #in_memory_docs | Returns the in-memory documents for an association value without triggering a database load of any unloaded documents. |
DSL Calls
included
[ GitHub ]66 67 68 69
# File 'lib/mongoid/association.rb', line 66
included do class_attribute :polymorphic self.polymorphic = false end
Class Attribute Details
._dependents_owner (rw)
[ GitHub ]# File 'lib/mongoid/association/depending.rb', line 18
class_attribute :_dependents_owner
._dependents_owner? ⇒ Boolean (rw)
[ GitHub ]# File 'lib/mongoid/association/depending.rb', line 18
class_attribute :_dependents_owner
.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 12
class_attribute :cyclic
.cyclic? ⇒ Boolean (rw)
[ GitHub ]# File 'lib/mongoid/association/embedded/cyclic.rb', line 12
class_attribute :cyclic
.dependents (rw)
[ GitHub ]# File 'lib/mongoid/association/depending.rb', line 11
class_attribute :dependents
.dependents? ⇒ Boolean (rw)
[ GitHub ]# File 'lib/mongoid/association/depending.rb', line 11
class_attribute :dependents
.embedded (rw)
[ GitHub ]# File 'lib/mongoid/association/macros.rb', line 11
class_attribute :, instance_reader: false
.embedded? ⇒ Boolean (rw)
[ GitHub ]# File 'lib/mongoid/association/macros.rb', line 11
class_attribute :, instance_reader: false
.embedded_relations (rw)
[ GitHub ]# File 'lib/mongoid/association/macros.rb', line 12
class_attribute :
.embedded_relations? ⇒ Boolean (rw)
[ GitHub ]# File 'lib/mongoid/association/macros.rb', line 12
class_attribute :
.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 64
attr_accessor :_association
#_dependents_owner (rw)
[ GitHub ]# File 'lib/mongoid/association/depending.rb', line 18
class_attribute :_dependents_owner
#_dependents_owner? ⇒ Boolean (rw)
[ GitHub ]# File 'lib/mongoid/association/depending.rb', line 18
class_attribute :_dependents_owner
#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 12
class_attribute :cyclic
#cyclic? ⇒ Boolean (rw)
[ GitHub ]# File 'lib/mongoid/association/embedded/cyclic.rb', line 12
class_attribute :cyclic
#dependents (rw)
[ GitHub ]# File 'lib/mongoid/association/depending.rb', line 11
class_attribute :dependents
#dependents? ⇒ Boolean (rw)
[ GitHub ]# File 'lib/mongoid/association/depending.rb', line 11
class_attribute :dependents
#embedded? ⇒ true | false (rw)
Determine if the document itself is embedded in another document via the proper channels. (If it has a parent document.)
# File 'lib/mongoid/association.rb', line 78
def @embedded ||= (cyclic ? _parent.present? : self.class.) end
#embedded_many? ⇒ true | false (readonly)
Determine if the document is part of an embeds_many association.
# File 'lib/mongoid/association.rb', line 88
def _association && _association.is_a?(Association::Embedded::EmbedsMany) end
#embedded_one? ⇒ true | false (readonly)
Determine if the document is part of an embeds_one association.
# File 'lib/mongoid/association.rb', line 98
def _association && _association.is_a?(Association::Embedded::EmbedsOne) end
#embedded_relations (rw)
[ GitHub ]# File 'lib/mongoid/association/macros.rb', line 12
class_attribute :
#embedded_relations? ⇒ Boolean (rw)
[ GitHub ]# File 'lib/mongoid/association/macros.rb', line 12
class_attribute :
#referenced_many? ⇒ true | false (readonly)
Determine if the document is part of an references_many association.
# File 'lib/mongoid/association.rb', line 123
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.
# File 'lib/mongoid/association.rb', line 133
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_name ⇒ Symbol
Get the association name for this document. If no association was defined an error will be raised.
# File 'lib/mongoid/association.rb', line 111
def association_name raise Errors::NoMetadata.new(self.class.name) unless _association _association.name end
#reload_relations ⇒ Hash
Convenience method for iterating through the loaded associations and reloading them.
# File 'lib/mongoid/association.rb', line 144
def reload_relations relations.each_pair do |name, | next unless instance_variable_defined?("@_#{name}") remove_instance_variable("@_#{name}") if _parent.nil? || instance_variable_get("@_#{name}") != _parent end end