Skip to Content Skip to Search

module ActiveRecord::Reflection::ClassMethods

Active Record Reflection

Reflection enables the ability to examine the associations and aggregations of Active Record classes and objects. This information, for example, can be used in a form builder that takes an Active Record object and creates input fields for all of the attributes depending on their type and displays the associations to other objects.

MacroReflection class has info for AggregateReflection and AssociationReflection classes.

Public instance methods

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

Account.reflect_on_aggregation(:balance) # => the balance AggregateReflection
Source code GitHub
# File activerecord/lib/active_record/reflection.rb, line 69
def reflect_on_aggregation(aggregation)
  aggregate_reflections[aggregation.to_s]
end

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

Source code GitHub
# File activerecord/lib/active_record/reflection.rb, line 61
def reflect_on_all_aggregations
  aggregate_reflections.values
end

Returns an array of AssociationReflection objects for all the associations in the class. If you only want to reflect on a certain association type, pass in the symbol (:has_many, :has_one, :belongs_to) as the first parameter.

Example:

Account.reflect_on_all_associations             # returns an array of all associations
Account.reflect_on_all_associations(:has_many)  # returns an array of all has_many associations
Source code GitHub
# File activerecord/lib/active_record/reflection.rb, line 106
def reflect_on_all_associations(macro = nil)
  association_reflections = reflections.values
  association_reflections.select! { |reflection| reflection.macro == macro } if macro
  association_reflections
end

Returns an array of AssociationReflection objects for all associations which have :autosave enabled.

Source code GitHub
# File activerecord/lib/active_record/reflection.rb, line 126
def reflect_on_all_autosave_associations
  reflections.values.select { |reflection| reflection.options[:autosave] }
end

Returns the AssociationReflection object for the association (use the symbol).

Account.reflect_on_association(:owner)             # returns the owner AssociationReflection
Invoice.reflect_on_association(:line_items).macro  # returns :has_many
Source code GitHub
# File activerecord/lib/active_record/reflection.rb, line 117
def reflect_on_association(association)
  reflections[association.to_s]
end

Returns a Hash of name of the reflection as the key and an AssociationReflection as the value.

Account.reflections # => {"balance" => AggregateReflection}
Source code GitHub
# File activerecord/lib/active_record/reflection.rb, line 77
def reflections
  @__reflections ||= begin
    ref = {}

    _reflections.each do |name, reflection|
      parent_reflection = reflection.parent_reflection

      if parent_reflection
        parent_name = parent_reflection.name
        ref[parent_name.to_s] = parent_reflection
      else
        ref[name] = reflection
      end
    end

    ref
  end
end

Definition files