Module: ActiveRecord::Reflection::ClassMethods

Defined in:
lib/active_record/reflection.rb

Overview

Reflection allows you to interrogate Active Record classes and objects about their associations and aggregations. This information can for example be used in a form builder that took an Active Record object and created input fields for all of the attributes depending on their type and displayed the associations to other objects.

You can find the interface for the AggregateReflection and AssociationReflection classes in the abstract MacroReflection class.

Instance Method Summary collapse

Instance Method Details

#reflect_on_aggregation(aggregation) ⇒ Object

Returns the AggregateReflection object for the named aggregation (use the symbol). Example:

Account.reflect_on_aggregation(:balance) # returns the balance AggregateReflection


49
50
51
# File 'lib/active_record/reflection.rb', line 49

def reflect_on_aggregation(aggregation)
  reflect_on_all_aggregations.find { |reflection| reflection.name == aggregation } unless reflect_on_all_aggregations.nil?
end

#reflect_on_all_aggregationsObject

Returns an array of AggregateReflection objects for all the aggregations in the class.



43
44
45
# File 'lib/active_record/reflection.rb', line 43

def reflect_on_all_aggregations
  read_inheritable_attribute "aggregations"
end

#reflect_on_all_associationsObject

Returns an array of AssociationReflection objects for all the aggregations in the class.



54
55
56
# File 'lib/active_record/reflection.rb', line 54

def reflect_on_all_associations
  read_inheritable_attribute "associations"
end

#reflect_on_association(association) ⇒ Object

Returns the AssociationReflection object for the named aggregation (use the symbol). Example:

Account.reflect_on_association(:owner) # returns the owner AssociationReflection


60
61
62
# File 'lib/active_record/reflection.rb', line 60

def reflect_on_association(association)
  reflect_on_all_associations.find { |reflection| reflection.name == association } unless reflect_on_all_associations.nil?
end