Overview
Associations in Mongoid allow you to create relationships between
models. When you define an association, Mongoid stores metadata about
that association. You can access the metadata by calling the
reflect_on_association method on a model class or document, or by
directly accessing the metadata on a specific document. The following
example shows how to access metadata by using the
reflect_on_association method and by direct access:
# Get the metadata for a named association from the class or document Model.reflect_on_association(:<association_name>) # Directly access metadata on a document model.associations[:<association_name>]
Note
Replace <association_name> in the preceding example with the name of your
association.
Attributes
All associations contain attributes that store information about the associated document. Associations contain the following attributes:
_target: The proxied document or documents_base: The document on which the association is defined_association: Information about the association
The following example accesses each of the preceding attributes:
class Band include Mongoid::Document embeds_many :songs end Band.songs = [ song ] Band.songs._target # returns [ song ] Band.songs._base # returns band Band.songs._association # returns the association metadata
The following table shows the information stored in the _association
attribute:
Method | Description |
|---|---|
| The name of the parent to a polymorphic child. |
| Returns whether an |
| Returns whether the association is autobuilding. |
| Returns whether the association is autosaving. |
| Returns whether the association has callbacks cascaded down from the parent. |
| The class name of the proxied document. |
| Returns whether the association is a cyclic association. |
| The association's dependent option. |
| Returns |
| Returns whether the association is embedded in another document. |
| Returns whether the association has a |
| The name of the foreign-key field. |
| The name of the foreign-key field's dirty-check method. |
| The name of the foreign-key field's setter. |
| Returns whether the foreign key is auto indexed. |
| The names of all inverse associations. |
| The name of a single inverse association. |
| The class name of the association on the inverse side. |
| The name of the foreign-key field on the inverse side. |
| The class of the association on the inverse side. |
| The metadata of the association on the inverse side. |
| The explicitly defined name of the inverse association. |
| The name of the method used to set the inverse. |
| The name of the polymorphic-type field of the inverse. |
| The name of the polymorphic-type field's setter of the inverse. |
| The name of the field in the attribute's hash that is used to get the association. |
| The class of the proxied documents in the association. |
| The association name. |
| Returns |
| The custom sorting options on the association. |
| Returns whether the association is polymorphic. |
| The name of the field to set the association. |
| The name of the attribute in which to store an embedded association. |
| Returns whether the association has a touch option. |
| The name of the field to get the polymorphic type. |
| The name of the field to set the polymorphic type. |
| Returns whether the association has an associated validation. |