类:Mongoid::Association::Many

继承:
代理
  • 对象
显示全部
扩展方式:
可转发
包括:
可枚举
定义于:
lib/mongoid/association/many.rb

Overview

这是所有多对一和多对多关联代理的超类。

常量摘要

Proxy继承的常量

Proxy::KEEPER_METHODS

实例属性摘要

Proxy继承的属性

#_association#_base#_target

实例方法摘要折叠

Proxy继承的方法

apply_ordering#extend_proxies#initialize#klass#reset_unloaded#substitutable

包含在封送处理中的方法

#marshal_dump#marshal_load

构造函数详情

该类从Mongoid::Association::Proxy继承了一个构造函数

实例方法详细信息

#空白?true | false

关联是否为空?

例子:

关联是否为空??

person.addresses.blank?

返回:

  • ( true | false )

    关联是否为空。



22
23
24
# File 'lib/mongoid/association/many.rb', line 22

def 空白?
  !任何?
end

# create (attributes = nil, type = nil, &block) ⇒文档

在引用许多关联上创建新文档。 如果父文档已持久保存,这将保存文档。

例子:

创建并保存新文档。

person.posts.create(:text => "Testing")

参数:

  • 属性 哈希 (默认为: nil

    创建时使用的属性。

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

    要创建的文档的可选类型。

返回:

  • (文档)

    新创建的文档。



36
37
38
39
40
41
42
43
44
# File 'lib/mongoid/association/many.rb', line 36

def 创建(属性 = nil, 类型 = nil, )
  if 属性.is_a?(::阵列)
    属性.map { |attrs| 创建(attrs, 类型, ) }
  else
    doc = 构建(属性, 类型, )
    _base.持续存在? ? doc.保存 : Raise_unsaved(doc)
    doc
  end
end

#创建! (attributes = nil, type = nil, &block) ⇒文档

在引用许多关联上创建新文档。 如果父文档已持久保存,这将保存文档;如果验证失败,则会引发错误。

例子:

创建并保存新文档。

person.posts.create!(:text => "Testing")

参数:

  • 属性 哈希 (默认为: nil

    创建时使用的属性。

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

    要创建的文档的可选类型。

返回:

  • (文档)

    新创建的文档。

引发:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mongoid/association/many.rb', line 59

def 创建!(属性 = nil, 类型 = nil, )
  if 属性.is_a?(::阵列)
    属性.map { |attrs| 创建!(attrs, 类型, ) }
  else
    doc = 构建(属性, 类型, )

    阵列(doc). do |doc|
      doc.try(:run_pending_callbacks)
    end

    _base.持续存在? ? doc.save! : Raise_unsaved(doc)
    doc
  end
end

# find_or_create_by (attrs = {}, type = nil, &block) ⇒文档

查找给定条件的第一个文档,或根据提供的条件创建新文档。

@param [ Hash ] attrs The attributes to search or create with.
@param [ Class ] type The optional type of document to create.

例子:

查找或创建。

person.posts.find_or_create_by(:title => "Testing")

返回:

  • (文档)

    现有文档或新创建的文档。



84
85
86
# File 'lib/mongoid/association/many.rb', line 84

def find_or_create_by(attrs = {}, 类型 = nil, )
  find_or(:create, attrs, 类型, )
end

# find_or_create_by! (attrs = {}, type = nil, &block) ⇒文档

查找给定条件的第一个文档,或根据提供的条件创建新文档。 如果验证失败,则会引发错误。

例子:

查找或创建。

person.posts.find_or_create_by!(:title => "Testing")

参数:

  • attrs 哈希 (默认为: {}

    要搜索或创建的属性。

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

    要创建的文档的可选类型。

返回:

  • (文档)

    现有文档或新创建的文档。

引发:



100
101
102
# File 'lib/mongoid/association/many.rb', line 100

def find_or_create_by!(attrs = {}, 类型 = nil, )
  find_or(:create!, attrs, 类型, )
end

# find_or_initialize_by (attrs = {}, type = nil, &block) ⇒文档

在给定的条件下查找第一个Document ,或使用所提供的条件实例化一个新文档

例子:

查找或初始化。

person.posts.find_or_initialize_by(:title => "Test")

参数:

  • attrs 哈希 (默认为: {}

    用于Atlas Search或初始化的属性。

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

    要构建的可选子类。

返回:

  • (文档)

    现有文档或新实例化的文档。



114
115
116
# File 'lib/mongoid/association/many.rb', line 114

def find_or_initialize_by(attrs = {}, 类型 = nil, )
  find_or(:build, attrs, 类型, )
end

#nil?false

此代理永远不能为零。

例子:

代理是否为零?

relation.nil?

返回:

  • ( false )

    始终为 false。



124
125
126
# File 'lib/mongoid/association/many.rb', line 124

def nil?
  false
end

# respond_to? (name, include_private = false) ⇒ true | false

由于method_missing被覆盖,我们也应该覆盖它。

例子:

代理是否响应该方法?

relation.respond_to?(:name)

参数:

  • 名称 (符号)

    方法名称。

  • include_private ( true | false ) (默认为: false

    是否包含私有方法。

返回:

  • ( true | false )

    如果代理响应该方法。



137
138
139
140
# File 'lib/mongoid/association/many.rb', line 137

def respond_to?(名称, include_private = false)
  [].respond_to?(名称, include_private) ||
    klass.respond_to?(名称, include_private) || 
end

#作用域">条件

这是该协会标准的公开访问权限。

例子:

获取作用域关联。

relation.scoped

返回:

  • ( Criteria )

    限定范围的条件。



148
149
150
# File 'lib/mongoid/association/many.rb', line 148

def 作用域
  条件
end

#serializable_hash(options = {}) ⇒ Hash

以可序列化哈希的形式获取文档,由 ActiveModel 的JSON和 XML 序列化器使用。 此覆盖只是为了能够传递 :include 和 : except 选项来获取哈希中的关联。

例子:

获取可序列化哈希值。

relation.serializable_hash

参数:

  • 选项 哈希 (默认为: {}

    要传递的选项。

选项哈希 ( options ):

  • :除了 ( Symbol | string | Array<Symbol | string > )

    请勿包含这些字段。

  • :include ( Symbol | string | Array<Symbol | string > )

    要包含哪些关联。

  • :only ( Symbol | string | Array<Symbol | string > )

    将字段限制为仅这些字段。

返回:

  • (哈希)

    文档已准备好进行序列化。



166
167
168
# File 'lib/mongoid/association/many.rb', line 166

def Serializable_hash(选项 = {})
  _target.map { |文档| 文档.Serializable_hash(选项) }
end

# unscoped条件

在未应用默认范围的情况下获取嵌入式文档的条件。

例子:

获取未指定范围的条件。

person.addresses.unscoped

返回:

  • ( Criteria )

    未限定范围的条件。



177
178
179
# File 'lib/mongoid/association/many.rb', line 177

def 未限定作用域
  条件.未限定作用域
end