跳到內文 跳到搜尋

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