模块:Mongoid::Association::Macros::ClassMethods

定义于:
lib/mongoid/association/macros.rb

Overview

用于关联的类方法。

实例方法摘要折叠

实例方法详细信息

# belongs_to (name, options = {}, &block) ⇒对象

将引用的关联从子文档添加到另一个数据库或集合中的文档。

例子:

定义关联。


class Game
  include Mongoid::Document
  belongs_to :person
end

class Person
  include Mongoid::Document
  has_one :game
end

参数:

  • 名称 (符号)

    关联的名称。

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

    关联选项。

  • 区块(&B)

    用于定义扩展的可选区块。



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

def belongs_to(名称, 选项 = {}, )
  define_association!(__method__, 名称, 选项, )
end

# embedded_in (name, options = {}, &block) ⇒对象

将关联添加回父文档。 要设立子文档对父文档的引用,必须使用该宏。 如果子对象未定义此关联,则在子对象上调用持久性方法将导致保存失败。

例子:

定义关联。


class Person
  include Mongoid::Document
  embeds_many :addresses
end

class Address
  include Mongoid::Document
  embedded_in :person
end

参数:

  • 名称 (符号)

    关联的名称。

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

    关联选项。

  • 区块(&B)

    用于定义扩展的可选区块。



80
81
82
# File 'lib/mongoid/association/macros.rb', line 80

def embedded_in(名称, 选项 = {}, )
  define_association!(__method__, 名称, 选项, )
end

# embeds_many (name, options = {}, &block) ⇒对象

添加父文档与其子文档的关联。 关联的名称必须是子类名称的复数形式。

例子:

定义关联。


class Person
  include Mongoid::Document
  embeds_many :addresses
end

class Address
  include Mongoid::Document
  embedded_in :person
end

参数:

  • 名称 (符号)

    关联的名称。

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

    关联选项。

  • 区块(&B)

    用于定义扩展的可选区块。



103
104
105
# File 'lib/mongoid/association/macros.rb', line 103

def embeds_many(名称, 选项 = {}, )
  define_association!(__method__, 名称, 选项, )
end

# embeds_one (name, options = {}, &block) ⇒对象

添加父文档与其子文档的关联。 关联名称必须是子类名称的单数形式。

例子:

定义关联。


class Person
  include Mongoid::Document
  embeds_one :name
end

class Name
  include Mongoid::Document
  embedded_in :person
end

参数:

  • 名称 (符号)

    关联的名称。

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

    关联选项。

  • 区块(&B)

    用于定义扩展的可选区块。



126
127
128
# File 'lib/mongoid/association/macros.rb', line 126

def embeds_one(名称, 选项 = {}, )
  define_association!(__method__, 名称, 选项, )
end

# has_and_belongs_to_many (name, options = {}, &block) ⇒对象

在此文档的许多部分与另一个文档的许多部分之间添加引用的多对多关联。

例子:

定义关联。


class Person
  include Mongoid::Document
  has_and_belongs_to_many :preferences
end

class Preference
  include Mongoid::Document
  has_and_belongs_to_many :people
end

参数:

  • 名称 (符号)

    关联的名称。

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

    关联选项。

  • 区块(&B)

    用于定义扩展的可选区块。



194
195
196
# File 'lib/mongoid/association/macros.rb', line 194

def has_and_belongs_to_many(名称, 选项 = {}, )
  define_association!(__method__, 名称, 选项, )
end

# has_many (name, options = {}, &block) ⇒对象

将引用的关联从父文档添加到另一个数据库或集合中的许多文档。

例子:

定义关联。


class Person
  include Mongoid::Document
  has_many :posts
end

class Game
  include Mongoid::Document
  belongs_to :person
end

参数:

  • 名称 (符号)

    关联的名称。

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

    关联选项。

  • 区块(&B)

    用于定义扩展的可选区块。



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

def has_many(名称, 选项 = {}, )
  define_association!(__method__, 名称, 选项, )
end

# has_one (name, options = {}, &block) ⇒对象

将引用的关联从子文档添加到另一个数据库或集合中的文档。

例子:

定义关联。


class Game
  include Mongoid::Document
  belongs_to :person
end

class Person
  include Mongoid::Document
  has_one :game
end

参数:

  • 名称 (符号)

    关联的名称。

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

    关联选项。

  • 区块(&B)

    用于定义扩展的可选区块。



216
217
218
# File 'lib/mongoid/association/macros.rb', line 216

def has_one(名称, 选项 = {}, )
  define_association!(__method__, 名称, 选项, )
end