Module: Mongoid::Association::Accessors

Extended by:
ActiveSupport::Concern
Included in:
Mongoid::Association
Defined in:
build/mongoid-master/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 (Association)

    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.

Since:

  • 2.0.0.rc.1



29
30
31
32
# File 'build/mongoid-master/lib/mongoid/association/accessors.rb', line 29

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 (Association)

    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.

Since:

  • 2.0.0.rc.1



48
49
50
51
52
# File 'build/mongoid-master/lib/mongoid/association/accessors.rb', line 48

def create_relation(object, association, selected_fields = nil)
  type = @attributes[association.inverse_type]
  target = association.build(self, object, type, selected_fields)
  target ? association.create_relation(self, target) : nil
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.

Since:

  • 3.0.14



63
64
65
66
67
# File 'build/mongoid-master/lib/mongoid/association/accessors.rb', line 63

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.

Since:

  • 2.0.0.rc.1



81
82
83
# File 'build/mongoid-master/lib/mongoid/association/accessors.rb', line 81

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