Class: Mongoid::Relations::Bindings::Referenced::ManyToMany

Inherits:
Mongoid::Relations::Binding show all
Defined in:
lib/mongoid/relations/bindings/referenced/many_to_many.rb

Overview

Binding class for all references_and_referenced_in_many relations.

Instance Attribute Summary

Attributes inherited from Mongoid::Relations::Binding

#base, #metadata, #target

Instance Method Summary (collapse)

Methods inherited from Mongoid::Relations::Binding

#binding, #initialize

Constructor Details

This class inherits a constructor from Mongoid::Relations::Binding

Instance Method Details

- (Object) bind_one(doc)

Binds a single document with the inverse relation. Used specifically when appending to the proxy.

Examples:

Bind one document.

person.preferences.bind_one(preference)

Parameters:

  • doc (Document)

    The single document to bind.

Since:

  • 2.0.0.rc.1



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongoid/relations/bindings/referenced/many_to_many.rb', line 19

def bind_one(doc)
  binding do
    inverse_keys = doc.you_must(.inverse_foreign_key)
    if inverse_keys
      unless inverse_keys.include?(inverse_record_id(doc))
        inverse_keys.push(inverse_record_id(doc))
      end
      doc.reset_relation_criteria(.inverse)
    end
    base.synced[.foreign_key] = true
    doc.synced[.inverse_foreign_key] = true
  end
end

- (Object) determine_inverse_metadata(doc)



62
63
64
# File 'lib/mongoid/relations/bindings/referenced/many_to_many.rb', line 62

def (doc)
  doc.relations[base.class.name.demodulize.underscore.pluralize]
end

- (Object) inverse_record_id(doc)

Find the inverse id referenced by inverse_keys



53
54
55
56
57
58
59
60
# File 'lib/mongoid/relations/bindings/referenced/many_to_many.rb', line 53

def inverse_record_id(doc)
   = (doc)
  if 
    base.__send__(.primary_key)
  else
    base.id
  end
end

- (Object) unbind_one(doc)

Unbind a single document.

Examples:

Unbind the document.

person.preferences.unbind_one(document)

Since:

  • 2.0.0.rc.1



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mongoid/relations/bindings/referenced/many_to_many.rb', line 39

def unbind_one(doc)
  binding do
    base.send(.foreign_key).delete_one(record_id(doc))
    inverse_keys = doc.you_must(.inverse_foreign_key)
    if inverse_keys
      inverse_keys.delete_one(inverse_record_id(doc))
      doc.reset_relation_criteria(.inverse)
    end
    base.synced[.foreign_key] = true
    doc.synced[.inverse_foreign_key] = true
  end
end