instance method preload_one_association

Ruby on Rails 3.0.20

Since v2.2.3 Last seen in v3.0.20 Private

Available in: v2.2.3 v2.3.18 v3.0.20

Signature

preload_one_association(records, association, preload_options={})

Preloads a specific named association for the given records. This is called by preload_associations as its base case.

Parameters

records req
association req
preload_options opt = {}
Source
# File activerecord/lib/active_record/association_preload.rb, line 111
      def preload_one_association(records, association, preload_options={})
        class_to_reflection = {}
        # Not all records have the same class, so group then preload
        # group on the reflection itself so that if various subclass share the same association then
        # we do not split them unnecessarily
        records.group_by { |record| class_to_reflection[record.class] ||= record.class.reflections[association]}.each do |reflection, _records|
          raise ConfigurationError, "Association named '#{ association }' was not found; perhaps you misspelled it?" unless reflection

          # 'reflection.macro' can return 'belongs_to', 'has_many', etc. Thus,
          # the following could call 'preload_belongs_to_association',
          # 'preload_has_many_association', etc.
          send("preload_#{reflection.macro}_association", _records, reflection, preload_options)
        end
      end

Defined in activerecord/lib/active_record/association_preload.rb line 111 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::AssociationPreload::ClassMethods

Type at least 2 characters to search.

↑↓ navigate · open · esc close