method
set_association_single_records
Ruby on Rails latest stable (v7.1.3.2)
-
0 notes -
Class: ActiveRecord::AssociationPreload::ClassMethods
set_association_single_records(id_to_record_map, reflection_name, associated_records, key)
private
Hide source
# File activerecord/lib/active_record/association_preload.rb, line 149 def set_association_single_records(id_to_record_map, reflection_name, associated_records, key) seen_keys = {} associated_records.each do |associated_record| #this is a has_one or belongs_to: there should only be one record. #Unfortunately we can't (in portable way) ask the database for #'all records where foo_id in (x,y,z), but please # only one row per distinct foo_id' so this where we enforce that next if seen_keys[associated_record[key].to_s] seen_keys[associated_record[key].to_s] = true mapped_records = id_to_record_map[associated_record[key].to_s] mapped_records.each do |mapped_record| association_proxy = mapped_record.send("set_#{reflection_name}_target", associated_record) association_proxy.__send__(:set_inverse_instance, associated_record, mapped_record) end end id_to_record_map.each do |id, records| next if seen_keys.include?(id.to_s) records.each {|record| record.send("set_#{reflection_name}_target", nil) } end end