diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-09-24 14:57:15 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-09-24 14:57:15 -0700 |
commit | c4e1fdd9b20020594135aaf5ae06ec1bec05df03 (patch) | |
tree | bc10b7b5a23d520766db262e57316947d9c5606c /activerecord/lib | |
parent | e9d2ff6959f1e3fcd4fed1d18d8de575aa8d36e8 (diff) | |
download | rails-c4e1fdd9b20020594135aaf5ae06ec1bec05df03.tar.gz rails-c4e1fdd9b20020594135aaf5ae06ec1bec05df03.tar.bz2 rails-c4e1fdd9b20020594135aaf5ae06ec1bec05df03.zip |
guarantee that `klass` is not nil inside the preloader objects
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/preloader.rb | 15 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/preloader/association.rb | 2 |
2 files changed, 12 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/associations/preloader.rb b/activerecord/lib/active_record/associations/preloader.rb index 9f03d60368..127d0e2642 100644 --- a/activerecord/lib/active_record/associations/preloader.rb +++ b/activerecord/lib/active_record/associations/preloader.rb @@ -133,7 +133,7 @@ module ActiveRecord 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 @@ module ActiveRecord end end - class NullPreloader + class AlreadyLoaded attr_reader :owners, :reflection def initialize(klass, owners, reflection, preload_scope) @@ -188,9 +188,16 @@ module ActiveRecord 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 diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb index d02384e221..63161d9744 100644 --- a/activerecord/lib/active_record/associations/preloader/association.rb +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -86,7 +86,7 @@ module ActiveRecord 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) |