Base 是 AggregateReflection 和 AssociationReflection 的基类。AggregateReflection 和 AssociationReflection 对象由 Reflection::ClassMethods 返回。
方法
Attributes
| [R] | active_record | |
| [R] | name | 返回宏的名称。
|
| [R] | options | 返回用于宏的选项的哈希。
|
| [R] | scope |
类公共方法
new(name, scope, options, active_record) 链接
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/reflection.rb, line 388 def initialize(name, scope, options, active_record) super() @name = name @scope = scope @options = normalize_options(options) @active_record = active_record @klass = options[:anonymous_class] @plural_name = active_record.pluralize_table_names ? name.to_s.pluralize : name.to_s end
实例公共方法
==(other_aggregation) 链接
如果 self 和 other_aggregation 具有相同的 name 属性、active_record 属性,并且 other_aggregation 具有已分配的选项哈希,则返回 true。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/reflection.rb, line 440 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 399 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 434 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 422 def klass @klass ||= _klass(class_name) end
scope_for(relation, owner = nil) 链接
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/reflection.rb, line 448 def scope_for(relation, owner = nil) relation.instance_exec(owner, &scope) || relation end