Base
类适用于 AggregateReflection 和 AssociationReflection。AggregateReflection 和 AssociationReflection 的对象由 Reflection::ClassMethods
返回。
属性
[R] | active_record | |
[R] | name | 返回宏的名称。
|
[R] | options | 返回用于宏的选项哈希。
|
[R] | scope |
类公共方法
new(name, scope, options, active_record) 链接
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/reflection.rb, line 376 def initialize(name, scope, options, active_record) super() @name = name @scope = scope @options = options @active_record = active_record @klass = options[:anonymous_class] @plural_name = active_record.pluralize_table_names ? name.to_s.pluralize : name.to_s validate_reflection! end
实例公共方法
==(other_aggregation) 链接
如果 self
和 other_aggregation
具有相同的 name
属性、active_record
属性,并且 other_aggregation
分配有选项哈希,则返回 true
。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/reflection.rb, line 421 def ==(other_aggregation) super || other_aggregation.kind_of?(self.class) && name == other_aggregation.name && !other_aggregation.options.nil? && active_record == other_aggregation.active_record end
autosave=(autosave) 链接
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/reflection.rb, line 388 def autosave=(autosave) @options[:autosave] = autosave parent_reflection = self.parent_reflection if parent_reflection parent_reflection.autosave = autosave end end
compute_class(name) 链接
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/reflection.rb, line 415 def compute_class(name) name.constantize end
klass() 链接
返回宏的类。
composed_of :balance, class_name: 'Money'
返回 Money 类 has_many :clients
返回 Client 类
class Company < ActiveRecord::Base
has_many :clients
end
Company.reflect_on_association(:clients).klass
# => Client
注意:不要调用 klass.new
或 klass.create
来实例化新的关联对象。请改用 build_association
或 create_association
。这允许插件挂接到关联对象创建。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/reflection.rb, line 411 def klass @klass ||= compute_class(class_name) end
scope_for(relation, owner = nil) 链接
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/reflection.rb, line 429 def scope_for(relation, owner = nil) relation.instance_exec(owner, &scope) || relation end