Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs Menu
Docs Home
/ /

Association Metadata

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.

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

Association#as

The name of the parent to a polymorphic child.

Association#as?

Returns whether an as option exists.

Association#autobuilding?

Returns whether the association is autobuilding.

Association#autosaving?

Returns whether the association is autosaving.

Association#cascading_callbacks?

Returns whether the association has callbacks cascaded down from the parent.

Association#class_name

The class name of the proxied document.

Association#cyclic?

Returns whether the association is a cyclic association.

Association#dependent

The association's dependent option.

Association#destructive?

Returns true if the association has a dependent delete or destroy method.

Association#embedded?

Returns whether the association is embedded in another document.

Association#forced_nil_inverse?

Returns whether the association has a nil inverse defined.

Association#foreign_key

The name of the foreign-key field.

Association#foreign_key_check

The name of the foreign-key field's dirty-check method.

Association#foreign_key_setter

The name of the foreign-key field's setter.

Association#indexed?

Returns whether the foreign key is auto indexed.

Association#inverses

The names of all inverse associations.

Association#inverse

The name of a single inverse association.

Association#inverse_class_name

The class name of the association on the inverse side.

Association#inverse_foreign_key

The name of the foreign-key field on the inverse side.

Association#inverse_klass

The class of the association on the inverse side.

Association#inverse_association

The metadata of the association on the inverse side.

Association#inverse_of

The explicitly defined name of the inverse association.

Association#inverse_setter

The name of the method used to set the inverse.

Association#inverse_type

The name of the polymorphic-type field of the inverse.

Association#inverse_type_setter

The name of the polymorphic-type field's setter of the inverse.

Association#key

The name of the field in the attribute's hash that is used to get the association.

Association#klass

The class of the proxied documents in the association.

Association#name

The association name.

Association#options

Returns self, for API compatibility with ActiveRecord.

Association#order

The custom sorting options on the association.

Association#polymorphic?

Returns whether the association is polymorphic.

Association#setter

The name of the field to set the association.

Association#store_as

The name of the attribute in which to store an embedded association.

Association#touchable?

Returns whether the association has a touch option.

Association#type

The name of the field to get the polymorphic type.

Association#type_setter

The name of the field to set the polymorphic type.

Association#validate?

Returns whether the association has an associated validation.

Back

Customize Association Behavior

On this page