类:Mongoid::Association::Referenced::BelongsTo::Binding

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

Overview

belongs_to 关联的绑定类。

实例属性摘要

Bindable中包含的属性

#_association#_base#_target

实例方法摘要折叠

Bindable中包含的方法

#bounding#initialize

实例方法详细信息

# bind_one对象

将基础对象绑定到关联的反向对象。 这样我们就可以在两侧引用实际对象本身。

这种情况会在反向对象和文档本身上设置关联。

例子:

装订文档。

game.person.bind(:continue => true)
game.person = Person.new


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mongoid/association/referenced/belongs_to/bounding.rb', line 22

def bind_one
  绑定 do
    check_poly态_inverses!(_target)
    bind_foreign_key(_base, record_id(_target))

    #设立反向类型(例如 "#{name}_type") 用于新的多态关联
    if _association.inverse_type && !_base.冻结?
      key = _association.解析器.default_key_for(_target)
      bind_poly态_inverse_type(_base, key)
    end

    if 反向 = _association.反向(_target)
      if set_base_association
        if _base.referenced_many?
          _target.__send__(反向).推动(_base)
        else
          remove_关联(_target)
          _target.set_relation(反向, _base)
        end
      end
    end
  end
end

# unbind_one对象

通过将引用设置为 nil 来解除基础对象和反向对象的绑定。

例子:

解除文档绑定。

game.person.unbind(:continue => true)
game.person = nil


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mongoid/association/referenced/belongs_to/bounding.rb', line 52

def unbind_one
  绑定 do
    反向 = _association.反向(_target)
    bind_foreign_key(_base, nil)
    bind_poly态_inverse_type(_base, nil)
    if 反向
      set_base_association
      if _base.referenced_many?
        _target.__send__(反向).删除(_base)
      else
        _target.set_relation(反向, nil)
      end
    end
  end
end