Module: Mongoid::Association::Accessors

Extended by:
ActiveSupport::Concern
Included in:
Mongoid::Association
Defined in:
lib/mongoid/association/accessors.rb

Overview

This module contains all the behavior related to accessing associations through the getters and setters, and how to delegate to builders to create new ones.

Instance Method Summary collapse

Instance Method Details

#__build__(name, object, association, selected_fields = nil) ⇒ Proxy

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

Examples:

Build the association.

person.__build__(:addresses, { :_id => 1 }, association)

Parameters:

  • name (String | Symbol)

    The name of the association.

  • object (Hash | BSON::ObjectId)

    The id or attributes to use.

  • association (Mongoid::Association::Relatable)

    The association metadata.

  • selected_fields (Hash) (defaults to: nil)

    Fields which were retrieved via #only. If selected_fields is specified, fields not listed in it will not be accessible in the built document.

Returns:

  • (Proxy)

    The association.



27
28
29
30
# File 'lib/mongoid/association/accessors.rb', line 27

def __build__(name, object, association, selected_fields = nil)
  relation = create_relation(object, association, selected_fields)
  set_relation(name, relation)
end

#create_relation(object, association, selected_fields = nil) ⇒ Proxy

Create an association from an object and association metadata.

Examples:

Create the association.

person.create_relation(document, association)

Parameters:

  • object (Document | Array<Document>)

    The association target.

  • association (Mongoid::Association::Relatable)

    The association metadata.

  • selected_fields (Hash) (defaults to: nil)

    Fields which were retrieved via #only. If selected_fields is specified, fields not listed in it will not be accessible in the created association document.

Returns:

  • (Proxy)

    The association.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mongoid/association/accessors.rb', line 44

def create_relation(object, association, selected_fields = nil)
  type = @attributes[association.inverse_type]
  target = if t = association.build(self, object, type, selected_fields)
    association.create_relation(self, t)
  else
    nil
  end

  # Only need to do this on embedded associations. The pending callbacks
  # are only added when materializing the documents, which only happens
  # on embedded associations. There is no call to the database in the
  # construction of a referenced association.
  if association.embedded?
    Array(target).each do |doc|
      doc.try(:run_pending_callbacks)
    end
  end

  target
end

#reset_relation_criteria(name) ⇒ Object

Resets the criteria inside the association proxy. Used by many-to-many associations to keep the underlying ids array in sync.

Examples:

Reset the association criteria.

person.reset_relation_criteria(:preferences)

Parameters:

  • name (Symbol)

    The name of the association.



72
73
74
75
76
# File 'lib/mongoid/association/accessors.rb', line 72

def reset_relation_criteria(name)
  if instance_variable_defined?("@_#{name}")
    send(name).reset_unloaded
  end
end

#set_relation(name, relation) ⇒ Proxy

Set the supplied association to an instance variable on the class with the provided name. Used as a helper just for code cleanliness.

Examples:

Set the proxy on the document.

person.set(:addresses, addresses)

Parameters:

  • name (String | Symbol)

    The name of the association.

  • relation (Proxy)

    The association to set.

Returns:

  • (Proxy)

    The association.



88
89
90
# File 'lib/mongoid/association/accessors.rb', line 88

def set_relation(name, relation)
  instance_variable_set("@_#{name}", relation)
end