instance method preload_has_one_association

Ruby on Rails 2.3.18

Since v2.2.3 Last seen in v3.0.20 Private

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

Signature

preload_has_one_association(records, reflection, preload_options={})

No documentation comment.

Parameters

records req
reflection req
preload_options opt = {}
Source
# File activerecord/lib/active_record/association_preload.rb, line 203
      def preload_has_one_association(records, reflection, preload_options={})
        return if records.first.send("loaded_#{reflection.name}?")
        id_to_record_map, ids = construct_id_map(records, reflection.options[:primary_key])
        options = reflection.options
        records.each {|record| record.send("set_#{reflection.name}_target", nil)}
        if options[:through]
          through_records = preload_through_records(records, reflection, options[:through])
          through_reflection = reflections[options[:through]]
          through_primary_key = through_reflection.primary_key_name
          unless through_records.empty?
            source = reflection.source_reflection.name
            through_records.first.class.preload_associations(through_records, source)
            if through_reflection.macro == :belongs_to
              rev_id_to_record_map, rev_ids = construct_id_map(records, through_primary_key)
              rev_primary_key = through_reflection.klass.primary_key
              through_records.each do |through_record|
                add_preloaded_record_to_collection(rev_id_to_record_map[through_record[rev_primary_key].to_s],
                                                   reflection.name, through_record.send(source))
              end
            else
              through_records.each do |through_record|
                add_preloaded_record_to_collection(id_to_record_map[through_record[through_primary_key].to_s],
                                                   reflection.name, through_record.send(source))
              end
            end
          end
        else
          set_association_single_records(id_to_record_map, reflection.name, find_associated_records(ids, reflection, preload_options), reflection.primary_key_name)
        end
      end

Defined in activerecord/lib/active_record/association_preload.rb line 203 · 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