Module: EagerRecord::EagerPreloading::AssociationProxyExtensions

Defined in:
lib/eager_record/eager_preloading.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



35
36
37
38
39
# File 'lib/eager_record/eager_preloading.rb', line 35

def self.included(base)
  base.module_eval do
    alias_method_chain :load_target, :eager_preloading
  end
end

Instance Method Details

#load_target_with_eager_preloadingObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/eager_record/eager_preloading.rb', line 41

def load_target_with_eager_preloading
  return nil unless defined?(@loaded)

  if !loaded? and (!@owner.new_record? || foreign_key_present)
    if originating_collection = @owner.instance_variable_get(:@originating_collection)
      association_name = @reflection.name
      @owner.class.__send__(:preload_associations, originating_collection, association_name)
      new_association = @owner.__send__(:association_instance_get, association_name)
      if new_association && __id__ != new_association.__id__ && new_association.loaded?
        @target = new_association.target
        @loaded = true
        return
      end
    end
  end
  load_target_without_eager_preloading
end