类:Mongoid::Association::Many

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

Overview

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

实例方法摘要折叠

实例方法详细信息

#空白?true | false

关联是否为空?

例子:

关联是否为空??

person.addresses.blank?

返回:

  • ( true | false )

    关联是否为空。



28
29
30
# File 'lib/mongoid/association/many.rb', line 28

def 空白?
  !任何?
end

#cache_version (timestamp_column = :updated_at) ⇒ string

为了与 Rails 的缓存兼容。根据给定时间戳返回一个字符串,并在版本中包含该关系中的记录数。

参数:

  • timestamp_column string | 符号 (默认为 :updated_at

    构造键时使用的时间戳列。

返回:

  • ( string )

    缓存版本字符串



195
196
197
198
# File 'lib/mongoid/association/many.rb', line 195

def cache_version(timestamp_column = :updated_at)
  @cache_version ||= {}
  @cache_version[timestamp_column] ||= Compute_cache_version(timestamp_column)
end

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

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

例子:

创建并保存新文档。

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

参数:

  • 属性 哈希 (默认为: nil

    创建时使用的属性。

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

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

返回:

  • (文档)

    新创建的文档。



42
43
44
45
46
47
48
49
50
# File 'lib/mongoid/association/many.rb', line 42

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

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

返回:

  • (文档)

    新创建的文档。

引发:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mongoid/association/many.rb', line 65

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 要搜索或创建的属性。@param [ Class ] type 要创建的document的可选类型。

例子:

查找或创建。

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

返回:

  • (文档)

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



90
91
92
# File 'lib/mongoid/association/many.rb', line 90

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

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

返回:

  • (文档)

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

引发:



106
107
108
# File 'lib/mongoid/association/many.rb', line 106

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

    要构建的可选子类。

返回:

  • (文档)

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



120
121
122
# File 'lib/mongoid/association/many.rb', line 120

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

#nil?false

此代理永远不能为零。

例子:

代理是否为零?

relation.nil?

返回:

  • ( false )

    始终为 false。



130
131
132
# File 'lib/mongoid/association/many.rb', line 130

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 )

    如果代理响应该方法。



143
144
145
146
# File 'lib/mongoid/association/many.rb', line 143

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

#作用域">条件

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

例子:

获取作用域关联。

relation.scoped

返回:

  • ( Criteria )

    限定范围的条件。



154
155
156
# File 'lib/mongoid/association/many.rb', line 154

def 作用域
  条件
end

#serializable_hash(options = {}) ⇒ Hash

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

例子:

获取可序列化哈希值。

relation.serializable_hash

参数:

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

    要传递的选项。

选项哈希 ( options ):

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

    请勿包含这些字段。

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

    要包含哪些关联。

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

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

返回:

  • (哈希)

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



172
173
174
# File 'lib/mongoid/association/many.rb', line 172

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

# unscoped条件

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

例子:

获取未指定范围的条件。

person.addresses.unscoped

返回:

  • ( Criteria )

    未限定范围的条件。



183
184
185
# File 'lib/mongoid/association/many.rb', line 183

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