Module: Mongoid::Relations::Accessors

Extended by:
ActiveSupport::Concern
Included in:
Mongoid::Relations
Defined in:
build/mongoid-6.4/lib/mongoid/relations/accessors.rb

Overview

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#__build__(name, object, metadata) ⇒ Proxy

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

Examples:

Build the relation.

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

Parameters:

  • name (String, Symbol)

    The name of the relation.

  • object (Hash, BSON::ObjectId)

    The id or attributes to use.

  • metadata (Metadata)

    The relation’s metadata.

Returns:

  • (Proxy)

    The relation.

Since:

  • 2.0.0.rc.1



24
25
26
27
# File 'build/mongoid-6.4/lib/mongoid/relations/accessors.rb', line 24

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

#create_relation(object, metadata) ⇒ Proxy

Create a relation from an object and metadata.

Examples:

Create the relation.

person.create_relation(document, )

Parameters:

Returns:

  • (Proxy)

    The relation.

Since:

  • 2.0.0.rc.1



40
41
42
43
44
# File 'build/mongoid-6.4/lib/mongoid/relations/accessors.rb', line 40

def create_relation(object, )
  type = @attributes[.inverse_type]
  target = .builder(self, object).build(type)
  target ? .relation.new(self, target, ) : nil
end

#reset_relation_criteria(name) ⇒ Object

Resets the criteria inside the relation proxy. Used by many to many relations to keep the underlying ids array in sync.

Examples:

Reset the relation criteria.

person.reset_relation_criteria(:preferences)

Parameters:

  • name (Symbol)

    The name of the relation.

Since:

  • 3.0.14



55
56
57
58
59
# File 'build/mongoid-6.4/lib/mongoid/relations/accessors.rb', line 55

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

#set_relation(name, relation) ⇒ Proxy

Set the supplied relation 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 relation.

  • relation (Proxy)

    The relation to set.

Returns:

  • (Proxy)

    The relation.

Since:

  • 2.0.0.rc.1



73
74
75
# File 'build/mongoid-6.4/lib/mongoid/relations/accessors.rb', line 73

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