класс ActiveRecord::Reflection::MacroReflection
Класс Base для AggregateReflection и AssociationReflection. Объекты AggregateReflection и AssociationReflection возвращаются методом Reflection::ClassMethods.
Атрибуты
Возвращает имя макроса.
composed_of :balance, class_name: 'Money' возвращает :balance has_many :clients возвращает :clients
Возвращает хеш параметров, используемых для макроса.
composed_of :balance, class_name: 'Money' возвращает { class_name: "Money" } has_many :clients возвращает {}
Открытые методы класса
# 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 Открытые методы экземпляра
# 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 Возвращает true, если self и other_aggregation имеют одинаковый атрибут name, атрибут active_record, а для other_aggregation задан хеш параметров.
# 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 # File activerecord/lib/active_record/reflection.rb, line 434 def compute_class(name) name.constantize end
# File activerecord/lib/active_record/reflection.rb, line 422 def klass @klass ||= _klass(class_name) end
Возвращает класс для макроса.
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. Это позволяет плагинам перехватывать создание объектов ассоциации.
# File activerecord/lib/active_record/reflection.rb, line 448 def scope_for(relation, owner = nil) relation.instance_exec(owner, &scope) || relation end
© 2004–2021 David Heinemeier Hansson
Licensed under the MIT License.