类:Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy
- 继承:
-
Referenced::HasMany::Proxy
- 对象
- Referenced::HasMany::Proxy
- Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy
- 扩展方式:
- 类方法
- 定义于:
- lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb
Overview
has_and_belongs_to_many 关联的透明代理。 对主题文档调用关联 getter 方法时,将返回此类的实例。 该类继承自 Mongoid::Association::Proxy,并将其大部分方法转发到关联的目标,即必须加载的对方集合上的文档大量。
在命名空间下定义
模块: 类方法
实例方法摘要折叠
-
# << (*args) ⇒ Array<Document> (也:#push)
将文档或文档数组附加到关联中。
-
# build (attributes = {}, type = nil) {|doc| ... } ⇒ 文档(也:#new)
从属性构建新文档并将其附加到此关联而不保存。
-
#concat(documents) ⇒ Array<Document>
将文档大量附加到关联中。
-
# 删除 (文档) ⇒ 文档(也:#delete_one)
从关联中删除文档。
-
# nullify (replacement = []) ⇒ 对象(也:#nullify_all、#clear、#purge)
通过删除外键和引用来删除基本文档和目标文档之间的所有关联,并在此过程中孤立目标文档。
-
# Replacement (replacement) ⇒ Many
用提供的目标文档替换关联中的现有文档。
-
# unscoped ⇒ 条件
获取未应用默认范围的文档条件。
类方法中包含的方法
实例方法详细信息
# << (*args) ⇒ Array< Document >也称为: push
将文档或文档数组附加到关联中。 将在此进程中设置父项并更新索引。
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb', line 56 def <<(*args) docs = args.展平 return concat(docs) if docs.size > 1 if (doc = docs.first) 附加(doc) do # 我们忽略对 中的外键值的更改 此代码区块中的 #changed_attributes 哈希有两个原因: # # 1 ) add_to_set 方法删除外部 # 键入 Changed_attributes 哈希值,但如果我们输入 # 方法,其外键值位于 # Changed_attributes 哈希,那么我们希望它存在于外部 # 也是这个方法。 它稍后会在 Syncable # 用于设置反向外键的模块。 # 2 ) reset_unloaded 方法访问外部 # _base 上的键,这会导致它被添加到 # Changed_attributes 哈希值。 发生这种情况是因为读取时 # 一个“可调整大小”属性,它会自动添加到 # Changed_attributes 哈希值。 这只适用于外国 # HABTM 关联的键值与其他关联相同 # 使用字符串作为外键值。 为了一致性 # 与其他关联,我们忽略此添加 # Changed_attributes 哈希值。 # 有关此问题的更长讨论,请参阅 MONGOID- 4843 。 reset_foreign_key_changes do _base.add_to_set(foreign_key => doc.public_send(_association.primary_key)) doc.保存 if child_persistable?(doc) reset_unloaded end end end 未同步(_base, foreign_key) 和 self end |
# build (attributes = {}, type = nil) {|doc| ... } ⇒文档也称为: new
从属性构建新文档并将其附加到此关联而不保存。
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb', line 120 def 构建(属性 = {}, 类型 = nil) doc = 工厂.execute_build(类型 || klass, 属性, execute_callbacks: false) 附加(doc) doc.apply_post_processed_defaults _base.public_send(foreign_key).推动(doc.public_send(_association.primary_key)) 未同步(doc, inverse_foreign_key) 产量(doc) if block_given? doc.run_pending_callbacks doc end |
# concat (documents) ⇒ Array< Document >
将文档数组附加到关联中。 对文档执行批量插入,而不是一次持久保存一个文档。
102 103 104 105 106 107 108 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb', line 102 def concat(文档) id, docs, inserts = {}, [], [] 文档.每 { |doc| append_document(doc, id, docs, inserts) } _base.推动(foreign_key => id.密钥) if 持久化? || _creating? persist_delayed(docs, inserts) self end |
# delete (document) ⇒ Document也称为: delete_one
从关联中删除文档。 这会将文档的外键设置为零。 如果关联上的依赖选项是 :delete_all 或 :destroy,则会进行相应的删除。
143 144 145 146 147 148 149 150 151 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb', line 143 def 删除(文档) doc = 超 if doc && 持久化? _base.pull(foreign_key => doc.public_send(_association.primary_key)) _target._unloaded = 条件 未同步(_base, foreign_key) end doc end |
# nullify (replacement = []) ⇒ Object也称为: nullify_all 、 clear 、 purge
通过删除外键和引用来删除基本文档和目标文档之间的所有关联,并在此过程中孤立目标文档。
165 166 167 168 169 170 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb', line 165 def nullify(替换 = []) _target.每 { |doc| execute_callback :before_remove, doc } cleanup_inverse_for(替换) 除非 _association.forced_nil_inverse? _base.集(foreign_key => _base.public_send(foreign_key).清除) if 持久化? clear_target_for_nullify end |
# Replacement (replacement) ⇒ Many
用提供的目标文档替换关联中的现有文档。 如果新目标为零,则执行必要的删除。
Person.preferences.substitute([ new_post ])
186 187 188 189 190 191 192 193 194 195 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb', line 186 def 替换(替换) purge(替换) if 替换.空白? reset_unloaded clear_foreign_key_changes else 推动(替换.compact.uniq) end self end |
# unscoped ⇒条件
获取未应用默认范围的文档条件。
204 205 206 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb', line 204 def 未限定作用域 klass.未限定作用域.any_in(_id: _base.public_send(foreign_key)) end |