Class: ActiveRecord::CompositeKeyPreloader
- Inherits:
-
Object
- Object
- ActiveRecord::CompositeKeyPreloader
- Defined in:
- lib/active_record/composite_key_preloader.rb
Overview
Modified ActiveRecord::Associations::Preloader::Association
Class Method Summary collapse
-
.call(records, association, composite_key) ⇒ Object
The CompositeKeyPreloader does the same as default rails preloader ( ActiveRecord::Associations::Preloader ) The difference: CompositeKeyPreloader allows to reference another table by multiple columns.
Instance Method Summary collapse
-
#initialize(klass, owners, reflection, composite_key) ⇒ CompositeKeyPreloader
constructor
A new instance of CompositeKeyPreloader.
- #run ⇒ Object
Constructor Details
#initialize(klass, owners, reflection, composite_key) ⇒ CompositeKeyPreloader
Returns a new instance of CompositeKeyPreloader.
21 22 23 24 25 26 27 |
# File 'lib/active_record/composite_key_preloader.rb', line 21 def initialize(klass, owners, reflection, composite_key) @klass = klass @owners = owners @reflection = reflection @preloaded_records = [] @composite_key = composite_key end |
Class Method Details
.call(records, association, composite_key) ⇒ Object
The CompositeKeyPreloader does the same as default rails preloader ( ActiveRecord::Associations::Preloader ) The difference: CompositeKeyPreloader allows to reference another table by multiple columns
13 14 15 16 17 18 19 |
# File 'lib/active_record/composite_key_preloader.rb', line 13 def self.call(records, association, composite_key) records_for_preload = records.reject { |record| record.association(association).loaded? } return if records_for_preload.blank? assoc = records.first.association(association) new(assoc.klass, records, assoc.reflection, composite_key).run end |
Instance Method Details
#run ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/active_record/composite_key_preloader.rb', line 29 def run records = load_records owners.each do |owner| owner_key = @composite_key.map { |key_name| owner[key_name] } associate_records_to_owner(owner, records[owner_key] || []) end end |