类:Mongoid::Association::Referenced::HasMany::Proxy

继承:
很多
  • 对象
显示全部
扩展方式:
Forwardable, ClassMethods
定义于:
lib/mongoid/association/referenced/has_many/proxy.rb

Overview

has_many 关联的透明代理。 对主题文档调用关联 getter 方法时,将返回此类的实例。 该类继承自 Mongoid::Association::Proxy,并将其大部分方法转发到关联的目标,即必须加载的对方集合上的文档大量。

在命名空间下定义

模块: 类方法

常量摘要

Proxy继承的常量

Proxy::KEEPER_METHODS

实例属性摘要

Proxy继承的属性

#_association#_base#_target

实例方法摘要折叠

类方法中包含的方法

ager_loader嵌入式?

Many继承的方法

#blank?#create#create! , #find_or_create_by , #find_or_create_by!#find_or_initialize_by#nil? , #respond_to?#scoped#serializable_hash

Proxy继承的方法

apply_ordering#extend_proxies#klass#reset_unloaded#substitutable

包含在封送处理中的方法

#marshal_dump#marshal_load

构造函数详情

#initialize (base, target,association) ⇒代理

实例化一个新的references_many 关联。 将在反向对象上设置外键和基数。

例子:

创建新关联。

Referenced::Many.new(base, target, association)

参数:



48
49
50
51
52
53
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 48

def 初始化(基础, 目标, 关联)
  枚举 = 有很多::可枚举.new(目标, 基础, 关联)
  (基础, 枚举, 关联) do
    Raise_mixed if klass.嵌入式? && !klass.循环?
  end
end

动态方法处理

此类通过method_missing方法处理动态方法

#method_missingCriteria |对象(私有)

如果目标数组没有响应提供的方法,则尝试在类上查找命名范围或条件,然后向那里发送调用。

如果数组上存在该方法,则使用默认代理行为。

TODO:确保我们一致使用 respond_to_missing

anywhere we define method_missing.

rubocop:disable Style/MissingRespondToMissing

参数:

  • 名称 (符号 | string )

    方法的名称。

  • *args ( Object... )

    方法参数

  • 区块(&B)

    要传递的可选区块。

返回:

  • ( Criteria | Object )

    Criteria 或来自目标的返回值。



465
466
467
468
469
470
471
472
473
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 465

ruby2_keywords def method_missing(名称, *args, )
  if _target.respond_to?(名称)
    _target.发送(名称, *args, )
  else
    klass.发送(:with_scope, 条件) do
      条件.public_send(名称, *args, )
    end
  end
end

实例方法详细信息

# << (*args) ⇒ Array< Document >也称为: push

将文档或文档数组附加到关联中。 将在此进程中设置父项并更新索引。

例子:

附加文档。

person.posts << post

推送文档。

person.posts.push(post)

与其他文档连接。

person.posts.concat([ post_one, post_two ])

参数:

  • *args (文档... )

    任意数量的文档。

返回:

  • ( Array< Document > )

    加载的Docs 。



70
71
72
73
74
75
76
77
78
79
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 70

def <<(*args)
  docs = args.展平
  return concat(docs) if docs.size > 1

  if (doc = docs.first)
    附加(doc)
    doc.保存 if 持久化? && !_Assigning? && !doc.已验证?
  end
  self
end

# 构建 (attributes = {}, type = nil) ⇒文档也称为: new

从属性构建新文档并将其附加到此关联而不保存。

例子:

在关联上构建新文档。

person.posts.build(:title => "A new post")

参数:

  • 属性 哈希 (默认为: {}

    新文档的属性。

  • 类型 ( class ) (默认为: nil

    要构建的可选子类。

返回:



115
116
117
118
119
120
121
122
123
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 115

def 构建(属性 = {}, 类型 = nil)
  工厂.execute_build(类型 || klass, 属性, execute_callbacks: false).点击 do |doc|
    附加(doc)
    doc.apply_post_processed_defaults
    产量 doc if block_given?
    doc.run_pending_callbacks
    doc.run_callbacks(:build) { doc }
  end
end

# concat (documents) ⇒ Array< Document >

将文档数组附加到关联中。 对文档执行批量插入,而不是一次持久保存一个文档。

例子:

与其他文档连接。

person.posts.concat([ post_one, post_two ])

参数:

  • 文档 ( Array< Document > )

    要添加的Docs 。

返回:



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 92

def concat(文档)
  docs, inserts = [], []
  文档. do |doc|
    来年 除非 doc

    附加(doc)
    save_or_delay(doc, docs, inserts) if 持久化?
  end

  persist_delayed(docs, inserts)
  self
end

# delete (document) ⇒ Document也称为: delete_one

从关联中删除文档。 这会将文档的外键设置为零。 如果关联上的依赖选项是 :delete_all 或 :destroy,则会进行相应的删除。

例子:

删除文档。

person.posts.delete(post)

参数:

  • 文档 (文档)

    要删除的文档。

返回:

  • (文档)

    匹配的文档。



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 137

def 删除(文档)
  execute_callbacks_round(:删除, 文档) do
    结果 = _target.删除(文档) do |doc|
      if doc
        unbind_one(doc)
        级联!(doc) 除非 _Assigning?
      end
    end

    结果.点击 { reset_unloaded }
  end
end

# delete_all (conditions = nil) ⇒ Integer

在给定的条件下,从数据库中删除所有相关文档。

例子:

删除关联中的所有文档。

person.posts.delete_all

有条件地删除关联中的所有文档。

person.posts.delete_all({ :title => "Testing" })

参数:

  • 条件 哈希 (默认为: nil

    可选删除条件。

返回:

  • ( Integer )

    删除的文档数量。



166
167
168
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 166

def delete_all(条件 = nil)
  remove_all(条件, :delete_all)
end

#destroy_all (conditions = nil) ⇒ Integer

在给定的条件下销毁数据库中的所有相关文档。

例子:

销毁关联中的所有文档。

person.posts.destroy_all

有条件地销毁关联中的所有文档。

person.posts.destroy_all({ :title => "Testing" })

参数:

  • 条件 哈希 (默认为: nil

    可选销毁条件。

返回:

  • ( Integer )

    销毁的文档数量。



182
183
184
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 182

def destroy_all(条件 = nil)
  remove_all(条件, :destroy_all)
end

#each (&block) ⇒数组 < Document >

注意:

这会将整个关联加载到内存中。

遍历关联中的每个文档并生成提供的区块。

例子:

遍历文档。

person.posts.each do |post|
  post.save
end

返回:

  • ( Array< Document > )

    加载的Docs 。



197
198
199
200
201
202
203
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 197

def ()
  if 
    _target.()
  else
    to_enum
  end
end

#是否存在? (id_or_conditions = :none) ⇒ true | false

确定数据库中是否存在此关联中的任何文档。

如果关联包含文档,但所有文档仅存在于应用程序中,即尚未持久保存到数据库中,则此方法返回 false。

即使关联已加载到内存中,此方法也会在每次调用时查询数据库。

例子:

是否存在持久化文档?

person.posts.exists?

参数:

  • id_or_conditions ( :none | nil | false | Hash | Object ) (默认为: :none

    当为 :none(默认)时,如果关联中存在任何持久化文档,则返回 true。 当 nil 或 false 时,将始终返回 false。 如果给定哈希值,则会在关联中的文档中查询是否存在与给定条件匹配的文档,如果存在匹配项,则返回 true。 任何其他参数都被解释为ID ,并查询关联中是否存在具有匹配_id的文档。

返回:

  • ( true | false )

    如果持久化文档存在,则为 true;如果不存在,则为 false。



227
228
229
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 227

def 存在吗?(id_or_conditions = : none)
  条件.存在吗?(id_or_conditions)
end

#find(*args) {|Object| ... } ⇒ Document | Array<Document> | nil

注意:

每个参数都可以是单独的 ID、ID 数组或嵌套数组。 每个数组都将被展平。

注意:

这会将匹配的文档保留在内存中,以便稍后迭代。

根据 ID 或条件在关联上查找匹配文档。

此方法委托给Mongoid::Criteria#find 。 如果没有为该方法指定区块,它将根据所提供的 _id 值返回一个或多个文档。

如果为该方法提供了区块,则它将返回当前 Criteria对象找到的第一个符合区块返回 true 值的文档。

例子:

通过 ID 查找。

person.posts.find(BSON::ObjectId.new)

按多个 ID 查找。

person.posts.find([ BSON::ObjectId.new, BSON::ObjectId.new ])

使用区块查找第一个匹配文档。

person.addresses.find { |addr| addr.state == 'CA' }

参数:

  • *args ( [ Object | Array<Object> ]... )

    id。

  • 区块(&B)

    要传递的可选区块。

产量:

  • ( Object )

    将每个可枚举元素生成到区块中。

返回:



262
263
264
265
266
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 262

def find(*args, )
  匹配 = 条件.find(*args, )
  阵列(匹配). { |doc| _target.推动(doc) }
  匹配
end

# nullifyObject也称为: nullify_all

通过删除外键和引用来删除基本文档和目标文档之间的所有关联,并在此过程中孤立目标文档。

例子:

取消关联。

person.posts.nullify


274
275
276
277
278
279
280
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 274

def nullify
  条件.update_all(foreign_key => nil)
  _target.清除 do |doc|
    unbind_one(doc)
    doc.Changed_attributes.删除(foreign_key)
  end
end

# purge =" Many " 也称为: clear

清除关联。 如果文档已持久保存,则会从数据库中删除这些文档。

例子:

清除关联。

person.posts.clear

返回:

  • ( Many )

    该关联已清空。



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 291

def purge
  return nullify 除非 _association.具有破坏性?

  after_remove_error = nil
  条件.delete_all
  很多 = _target.清除 do |doc|
    execute_callback :before_remove, doc
    unbind_one(doc)
    doc.销毁 = true
    开始
      execute_callback :after_remove, doc
    救援 StandardError => e
      after_remove_error = e
    end
  end

  提高 after_remove_error if after_remove_error

  很多
end

# Replacement (replacement) ⇒ Many

用提供的目标文档替换关联中的现有文档。 如果新目标为零,则执行必要的删除。

例子:

替换关联。

person.posts.substitute([ new_post ])

参数:

  • 替换 ( Array< Document > )

    替换目标。

返回:

  • ( Many )

    关联。



324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 324

def 替换(替换)
  if 替换
    new_docs, docs = 替换.compact, []
    new_id = new_docs.map(:_id)
    remove_not_in(new_id)
    new_docs. do |doc|
      docs.推动(doc) if doc.发送(foreign_key) != _base.发送(_association.primary_key)
    end
    concat(docs)
  else
    purge
  end
  self
end

# unscoped条件

获取未应用默认范围的文档条件。

例子:

获取未指定范围的条件。

person.posts.unscoped

返回:

  • ( Criteria )

    未限定范围的条件。



346
347
348
# File 'lib/mongoid/association/referenced/has_many/proxy.rb', line 346

def 未限定作用域
  klass.未限定作用域.WHERE(foreign_key => _base.发送(_association.primary_key))
end