Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

guarantee that `klass` is not nil inside the preloader objects

  • Loading branch information...
commit c4e1fdd9b20020594135aaf5ae06ec1bec05df03 1 parent e9d2ff6
@tenderlove tenderlove authored
View
15 activerecord/lib/active_record/associations/preloader.rb
@@ -133,7 +133,7 @@ def preloaders_for_hash(association, records, scope)
def preloaders_for_one(association, records, scope)
grouped_records(association, records).flat_map do |reflection, klasses|
klasses.map do |rhs_klass, rs|
- loader = preloader_for(reflection, rs).new(rhs_klass, rs, reflection, scope)
+ loader = preloader_for(reflection, rs, rhs_klass).new(rhs_klass, rs, reflection, scope)
loader.run self
loader
end
@@ -173,7 +173,7 @@ def association_klass(reflection, record)
end
end
- class NullPreloader
+ class AlreadyLoaded
attr_reader :owners, :reflection
def initialize(klass, owners, reflection, preload_scope)
@@ -188,9 +188,16 @@ def preloaded_records
end
end
- def preloader_for(reflection, owners)
+ class NullPreloader
+ def self.new(klass, owners, reflection, preload_scope); self; end
+ def self.run(preloader); end
+ end
+
+ def preloader_for(reflection, owners, rhs_klass)
+ return NullPreloader unless rhs_klass
+
if owners.first.association(reflection.name).loaded?
- return NullPreloader
+ return AlreadyLoaded
end
case reflection.macro
View
2  activerecord/lib/active_record/associations/preloader/association.rb
@@ -86,7 +86,7 @@ def associated_records_by_owner(preloader)
h[owner] = []
end
- if klass && owner_keys.any?
+ if owner_keys.any?
# Some databases impose a limit on the number of ids in a list (in Oracle it's 1000)
# Make several smaller queries if necessary or make one query if the adapter supports it
sliced = owner_keys.each_slice(klass.connection.in_clause_length || owner_keys.size)
Please sign in to comment.
Something went wrong with that request. Please try again.