类:Mongoid::Association::Referenced::BelongsTo

继承:
对象
  • 对象
显示全部
包括:
Buildable , Mongoid::Association::Relatable
定义于:
lib/mongoid/association/referenced/belongs_to.rb ,
lib/mongoid/association/referenced/belongs_to/eager.rb,
lib/mongoid/association/referenced/belongs_to/proxy.rb,
lib/mongoid/association/referenced/belongs_to/bounding.rb,
lib/mongoid/association/referenced/belongs_to/buildable.rb

Overview

BelongsTo 类型关联。

在命名空间下定义

模块: 可构建 类: Binding 、 Eager 、 Proxy

常量摘要折叠

ASSOCIATION_OPTIONS =

除常用选项外,还可用于此类关联的选项。

返回:

  • ( Array<Symbol> ) —

    额外的有效选项。

%i[
  autobuild
  autosave
  counter_cache
  依赖
  foreign_key
  索引(index)
  多态
  primary_key
  touch
  可选
  必需
  范围
].冻结
VALID_OPTIONS =

此关联的有效选项的完整列表,包括共享选项。

返回:

  • ( Array<Symbol> ) —

    有效选项。

(ASSOCIATION_OPTIONS + SHARED_OPTIONS).冻结
FOREIGN_KEY_FIELD_TYPE =

保存外键的字段的类型。

返回:

  • (对象)
对象
FOREIGN_KEY_SUFFIX =

默认外键后缀。

返回:

  • ( string ) —

    '_id'

'_id'

来自Mongoid::Association::Relatable的常量

Mongoid::Association::Relatable::PRIMARY_KEY_DEFAULT 、 Mongoid::Association::Relatable::SHARED_OPTIONS

实例属性摘要

Mongoid::Association::Relatable 中包含的属性

#name、#options、#owner_class、#parent_inclusions

实例方法摘要折叠

Buildable中包含的方法

#build

Mongoid::Association::Relatable 中包含的方法

#== , #bindable? 、 #counter_cache_column_name 、 #create_relation 、 #什么呢? 、 #extension 、 #foreign_key_check 、 #foreign_key_setter 、 #get_callbacks 、 #in_to? 、 #initialize 、 #inverse 、 #inverse_association 、 #inverse_class 、 #inverse_class_name 、 #inverse_setter 、 #inverse_type_setter 、 #inverses 、 #key 、 #many? , #one? 、# relation_class 、# relation_class_name 、#setter、# type_setter 、 # validate?

选项中包含的方法

#as , #autoBuilding? 、 #autosave 、 #cascading_callbacks? , #counter_cached? , #cycle? 、#depend、# forced_nil_inverse ? , #indexed? 、#inverse_of、# order 、#primary_key、# store_as 、 # touch_field 、 # touchable? , #type

Constrainable中包含的方法

#convert_to_foreign_key

实例方法详细信息

# embedded? ⇒ false

此关联类型是否为嵌入式?

返回:

  • ( false ) —

    始终为 false。



77
78
79
# File 'lib/mongoid/association/referenced/belongs_to.rb', line 77

def 嵌入式?
  false
end

# foreign_key ⇒ string

获取用于保存关联引用的外键字段。

返回:

  • ( string ) —

    用于保存关联引用的外键字段。



91
92
93
94
95
96
97
# File 'lib/mongoid/association/referenced/belongs_to.rb', line 91

def foreign_key
  @foreign_key ||= if @options[:foreign_key]
                     @options[:foreign_key].to_s
                   else
                     default_foreign_key_field
                   end
end

#inverse_type ⇒ String

用于存储多态关联类型的字段的名称。

返回:

  • ( string ) —

    该字段用于存储多态关联的类型。



131
132
133
# File 'lib/mongoid/association/referenced/belongs_to.rb', line 131

def inverse_type
  (@inverse_type ||= "#{name}_type") if 多态?
end

#nested_builder (attributes, options) ⇒ Association::Nested::One

嵌套构建器对象。

参数:

  • 属性 (哈希) —

    用于构建关联对象的属性。

  • 选项 (哈希) —

    关联的选项。

返回:



141
142
143
# File 'lib/mongoid/association/referenced/belongs_to.rb', line 141

def nested_builder(属性, 选项)
  嵌套::one.new(self, 属性, 选项)
end

#路径(文档)→根

获取所提供文档的路径计算器。

例子:

获取路径计算器。

association.path(document)

参数:

  • 文档 (文档) —

    要计算的文档。

返回:

  • ( Root ) —

    根原子路径计算器。



153
154
155
# File 'lib/mongoid/association/referenced/belongs_to.rb', line 153

def 路径(文档)
  Mongoid::原子性::路径::根.new(文档)
end

#多态? ⇒ true | false

此关联是否具有多态性?

返回:

  • ( true | false ) —

    此关联是否为多态。



109
110
111
# File 'lib/mongoid/association/referenced/belongs_to.rb', line 109

def 多态?
  @poly态 ||= !!@options[:poly态]
end

#关系⇒ Association::BelongsTo::Proxy

获取此关联类型的关联代理类。

返回:

  • ( Association::BelongsTo::Proxy ) —

    代理类。



102
103
104
# File 'lib/mongoid/association/referenced/belongs_to.rb', line 102

def 关系
  代理
end

# relation_complements ⇒ Array< Mongoid::Association::Relatable >

关联补充列表。

返回:



54
55
56
# File 'lib/mongoid/association/referenced/belongs_to.rb', line 54

def lation_complements
  @relation_complements ||= [ 有很多, 有一个 ].冻结
end

# 解析器 ⇒ nil | Mongoid::ModelResolver

返回负责将多态类型引用转换为类对象的对象,反之亦然。它可通过定义关联时提供的 :polymorphic 选项获得。

请参阅 Mongoid::ModelResolver.resolver 以了解此处如何解释 :polymorphic 选项。

返回:

引发:

  • 如果给定标识符下没有注册此类解析程序,则出现 KeyError 错误。



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

def 解析器
  @resolver ||= Mongoid::ModelResolver.解析器(@options[:poly态])
end

#scope ⇒ Proc | Symbol | nil

获取查询关联时要应用的范围。

返回:

  • (Proc | Symbol | nil) —

    关联范围(如果有)。



160
161
162
# File 'lib/mongoid/association/referenced/belongs_to.rb', line 160

def 范围
  @options[:scope]
end

# setup! ⇒ self

在关联所属类上设置实例方法、字段等。

返回:

  • ( self )


61
62
63
64
65
# File 'lib/mongoid/association/referenced/belongs_to.rb', line 61

def setup!
  setup_instance_methods!
  @owner_class.aliased_fields[名称.to_s] = foreign_key
  self
end

#stores_foreign_key? ⇒ true

此关联类型是否存储外键?

返回:

  • ( true ) —

    始终为 true。



70
71
72
# File 'lib/mongoid/association/referenced/belongs_to.rb', line 70

def store_foreign_key?
  true
end

# validation_default ⇒ false

用于验证关联对象的默认。

返回:

  • ( false ) —

    始终为 false。



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

def validation_default
  false
end