类:Mongoid::Association::Referenced::HasAndBelongsToMany::Binding

继承:
对象
  • 对象
显示全部
包括:
Bindable
定义于:
lib/mongoid/association/referenced/has_and_belongs_to_many/ Binding.rb

Overview

所有 has_and_belongs_to_many 关联的绑定类。

实例属性摘要

Bindable中包含的属性

#_association#_base#_target

实例方法摘要折叠

Bindable中包含的方法

#bounding#initialize

实例方法详细信息

# bind_one (doc) ⇒对象

使用反向关联绑定单个文档。 专门在附加到代理时使用。

例子:

绑定一个文档。

person.preferences.bind_one(preference)

参数:

  • doc (文档)

    要绑定的单个文档。



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/ Binding.rb', line 20

def bind_one(doc)
  绑定 do
    inverse_keys = try_method(doc, _association.inverse_foreign_key) 除非 doc.冻结?
    if inverse_keys
      record_id = inverse_record_id(doc)
      除非 inverse_keys.包括?(record_id)
        try_method(doc, _association.inverse_foreign_key_setter, inverse_keys.推动(record_id))
      end
      doc.reset_relation_criteria(_association.反向)
    end
    _base._synced[_association.foreign_key] = true
    doc._synced[_association.inverse_foreign_key] = true
  end
end

# describe_inverse_association (doc) ⇒ Mongoid ::Association::Relatable

查找给定文档的反向关联。

参数:

返回:



77
78
79
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/ Binding.rb', line 77

def describe_inverse_association(doc)
  doc.关系[_base.class.名称.解调.下划线.复数化]
end

#inverse_record_id(doc) ⇒ BSON::ObjectId

查找 inverse_keys 引用的反向ID

参数:

返回:

  • ( BSON::ObjectId )

    反向ID 。



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/ Binding.rb', line 58

def inverse_record_id(doc)
  if pk = _association.选项[:inverse_primary_key]
    _base.发送(pk)
  else
    inverse_association = describe_inverse_association(doc)
    if inverse_association
      _base.__send__(inverse_association.primary_key)
    else
      _base._id
    end
  end
end

#unbind_one(doc) ⇒ Object

取消绑定单个文档。

例子:

解除文档绑定。

person.preferences.unbind_one(document)


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/ Binding.rb', line 39

def unbind_one(doc)
  绑定 do
    _base.发送(_association.foreign_key).delete_one(record_id(doc))
    inverse_keys = try_method(doc, _association.inverse_foreign_key) 除非 doc.冻结?
    if inverse_keys
      inverse_keys.delete_one(inverse_record_id(doc))
      doc.reset_relation_criteria(_association.反向)
    end
    _base._synced[_association.foreign_key] = true
    doc._synced[_association.inverse_foreign_key] = true
  end
end