模块:Mongoid::Association::Referenced::Syncable
- 包含在:
- Mongoid::Association
- 定义于:
- lib/mongoid/association/referenced/syncable.rb
Overview
此模块处理在多对多关联的双方之间同步外键的行为。
在命名空间下定义
模块: 类方法
实例方法摘要折叠
-
# _syncable? (association) ⇒ true | false
文档能否反向同步? 仅当键已更改且关联绑定尚未运行才会出现这种情况。
-
# _synced ⇒ 哈希
获取同步的外键。
-
# _synced? (foreign_key) ⇒ true | false
文档是否已针对外键进行同步?
-
# remove_inverse_keys (association) ⇒ 对象
在销毁时更新反向键。
-
# update_inverse_keys (association) ⇒ 对象
更新关联的反向键。
实例方法详细信息
# _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 ⇒哈希
获取同步的外键。
31 32 33 |
# File 'lib/mongoid/association/referenced/syncable.rb', line 31 def _synced @_synced ||= {} end |
# _synced? (foreign_key) ⇒ 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) ⇒对象
在销毁时更新反向键。
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) ⇒对象
更新关联的反向键。
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, 增加)..add_to_set(关联.inverse_foreign_key => _id) end 除非 subs.空? 关联.条件(self, subs)..pull(关联.inverse_foreign_key => _id) end end end |