模块:Mongoid::Association::Referenced::Syncable

包含在:
Mongoid::Association
定义于:
lib/mongoid/association/referenced/syncable.rb

Overview

此模块处理在多对多关联的双方之间同步外键的行为。

在命名空间下定义

模块: 类方法

实例方法摘要折叠

实例方法详细信息

# _syncable? (association) ⇒ true | false

文档能否反向同步? 仅当键已更改且关联绑定尚未运行才会出现这种情况。

例子:

外键是否可同步?

document._syncable?(association)

参数:

返回:

  • ( true | false )

    如果我们可以同步的话。



21
22
23
# File 'lib/mongoid/association/referenced/syncable.rb', line 21

def _syncable?(关联)
  !_synced?(关联.foreign_key) && 发送(关联.foreign_key_check)
end

# _synced哈希

获取同步的外键。

例子:

获取同步的外键。

document._synced

返回:

  • (哈希)

    同步的外键。



31
32
33
# File 'lib/mongoid/association/referenced/syncable.rb', line 31

def _synced
  @_synced ||= {}
end

# _synced? (foreign_key) ⇒ true | false

文档是否已针对外键进行同步?

例子:

文档是否已同步?

document._synced?

参数:

  • foreign_key ( string )

    外键。

返回:

  • ( true | false )

    如果我们可以同步的话。



43
44
45
# File 'lib/mongoid/association/referenced/syncable.rb', line 43

def _synced?(foreign_key)
  !!_synced[foreign_key]
end

# remove_inverse_keys (association) ⇒对象

在销毁时更新反向键。

例子:

更新反向键。

document.remove_inverse_keys(association)

参数:

返回:

  • ( Object )

    更新后的值。



55
56
57
58
59
60
# File 'lib/mongoid/association/referenced/syncable.rb', line 55

def remove_inverse_keys(关联)
  foreign_keys = 发送(关联.foreign_key)
  除非 foreign_keys.nil? || foreign_keys.空?
    关联.条件(self, foreign_keys).pull(关联.inverse_foreign_key => _id)
  end
end

# update_inverse_keys (association) ⇒对象

更新关联的反向键。

例子:

更新反向键

document.update_inverse_keys(association)

参数:

返回:

  • ( Object )

    更新后的值。



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mongoid/association/referenced/syncable.rb', line 70

def update_inverse_keys(关联)
  if previous_changes.has_key?(关联.foreign_key)
    , new = previous_changes[关联.foreign_key]
    增加, subs = new - ( || []), ( || []) - new

    # 如果是自动保存,则不希望添加重复项 - the
    # $addToSet 会先运行,然后 $push 和 $each
    # inverse on the autosave 会导致此问题。 我们删除每个ID
    # 如果同时使用 ID 加法和对象加法,内存中的内容是什么
    发生了 # 次。
    if 关联.autosave?
      发送(关联.名称).in_memory. do |doc|
        增加.delete_one(doc._id)
      end
    end

    除非 增加.空?
      关联.条件(self, 增加).without_options.add_to_set(关联.inverse_foreign_key => _id)
    end
    除非 subs.空?
      关联.条件(self, subs).without_options.pull(关联.inverse_foreign_key => _id)
    end
  end
end