Class: Mongoid::Relations::One

Inherits:
Proxy show all
Defined in:
lib/mongoid/relations/one.rb

Overview

This is the superclass for one to one relations and defines the common behaviour or those proxies.

Instance Attribute Summary

Attributes inherited from Proxy

#base, #loaded, #metadata, #target

Instance Method Summary collapse

Methods inherited from Proxy

#init

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mongoid::Relations::Proxy

Instance Method Details

#load!(options = {}) ⇒ One

Will load the target into an array if the target had not already been loaded.

Examples:

Load the relation into memory.

relation.load!

Returns:

  • (One)

    The relation.

Since:

  • 2.0.0.rc.5



18
19
20
21
22
23
24
25
# File 'lib/mongoid/relations/one.rb', line 18

def load!(options = {})
  tap do |relation|
    unless relation.loaded?
      relation.bind(options)
      relation.loaded = true
    end
  end
end

#substitute(new_target, options = {}) ⇒ Document?

Substitutes the supplied target documents for the existing document in the relation.

Examples:

Substitute the new document.

person.name.substitute(new_name)

Parameters:

  • other (Document)

    A document to replace the target.

Returns:

  • (Document, nil)

    The relation or nil.

Since:

  • 2.0.0.rc.1



38
39
40
41
42
43
44
# File 'lib/mongoid/relations/one.rb', line 38

def substitute(new_target, options = {})
  old_target = target
  tap do |relation|
    relation.target = new_target
    new_target ? bind(options) : (unbind(old_target, options) and return nil)
  end
end