aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorMingdong Luo <mdluo@nsmss.com>2015-01-31 19:34:56 -0800
committerMingdong Luo <mdluo@nsmss.com>2015-01-31 19:34:56 -0800
commit82e710f8471622bd3f3d1640e42ad14f9c9309b9 (patch)
tree233466527b797fe3ea7c6a7a3673795cea28aebe /activerecord/lib
parent70ac072976c8cc6f013f0df3777e54ccae3f4f8c (diff)
parent549d171a90135999e3c670f489494b7a39dd6dd7 (diff)
downloadrails-82e710f8471622bd3f3d1640e42ad14f9c9309b9.tar.gz
rails-82e710f8471622bd3f3d1640e42ad14f9c9309b9.tar.bz2
rails-82e710f8471622bd3f3d1640e42ad14f9c9309b9.zip
Merge pull request #1 from mdluo/pr/18316
Fix n+1 query problem when eager loading nil associations (fixes #18312)
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/associations/join_dependency.rb7
1 files changed, 6 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb
index 4b75370171..fcf06323e6 100644
--- a/activerecord/lib/active_record/associations/join_dependency.rb
+++ b/activerecord/lib/active_record/associations/join_dependency.rb
@@ -232,6 +232,7 @@ module ActiveRecord
end
def construct(ar_parent, parent, row, rs, seen, model_cache, aliases)
+ return if ar_parent.nil?
primary_id = ar_parent.id
parent.children.each do |node|
@@ -248,7 +249,11 @@ module ActiveRecord
key = aliases.column_alias(node, node.primary_key)
id = row[key]
- next if id.nil?
+ if id.nil?
+ nil_association = ar_parent.association(node.reflection.name)
+ nil_association.loaded!
+ next
+ end
model = seen[parent.base_klass][primary_id][node.base_klass][id]