instance method construct_id_map

Ruby on Rails 2.2.3

Since v2.2.3 Last seen in v3.0.20 Private

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

Signature

construct_id_map(records, primary_key=nil)

Given a collection of ActiveRecord objects, constructs a Hash which maps the objects’ IDs to the relevant objects. Returns a 2-tuple (id_to_record_map, ids) where id_to_record_map is the Hash, and ids is an Array of record IDs.

Parameters

records req
primary_key opt = nil
Source
# File activerecord/lib/active_record/association_preload.rb, line 164
      def construct_id_map(records, primary_key=nil)
        id_to_record_map = {}
        ids = []
        records.each do |record|
          primary_key ||= record.class.primary_key
          ids << record[primary_key]
          mapped_records = (id_to_record_map[ids.last.to_s] ||= [])
          mapped_records << record
        end
        ids.uniq!
        return id_to_record_map, ids
      end

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