跳至内容 跳至搜索

Base 类是 AggregateReflection 和 AssociationReflection 的基类。 AggregateReflection 和 AssociationReflection 的对象由 Reflection::ClassMethods 返回。

方法
#
A
C
K
N
S

属性

[R] active_record
[R] name

返回宏的名称。

composed_of :balance, class_name: 'Money' 返回 :balance has_many :clients 返回 :clients

[R] options

返回用于宏的选项哈希。

composed_of :balance, class_name: 'Money' 返回 { class_name: "Money" } has_many :clients 返回 {}

[R] scope

类公共方法

new(name, scope, options, active_record)

# 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)

如果 selfother_aggregation 具有相同的 name 属性、active_record 属性,并且 other_aggregation 具有分配给它的选项哈希,则返回 true

# 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)

# 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)

# 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.newklass.create 来实例化新的关联对象。 请使用 build_associationcreate_association。 这允许插件钩入关联对象创建。

# File activerecord/lib/active_record/reflection.rb, line 422
def klass
  @klass ||= _klass(class_name)
end

scope_for(relation, owner = nil)

# File activerecord/lib/active_record/reflection.rb, line 448
def scope_for(relation, owner = nil)
  relation.instance_exec(owner, &scope) || relation
end