aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorSammy Larbi <sam@codeodor.com>2015-01-03 13:30:30 -0600
committerSammy Larbi <sam@codeodor.com>2015-01-03 13:31:54 -0600
commitc840b18ac31a852d99ff760229f2c087b6961727 (patch)
tree5be634d408733d781de4fca2b77870b6fed3e026 /activerecord/lib/active_record
parent02e72a49d1c99084fef2c78c3a194e03b879099d (diff)
downloadrails-c840b18ac31a852d99ff760229f2c087b6961727.tar.gz
rails-c840b18ac31a852d99ff760229f2c087b6961727.tar.bz2
rails-c840b18ac31a852d99ff760229f2c087b6961727.zip
Fix n+1 query problem when eager loading nil associations (fixes #18312)
Diffstat (limited to 'activerecord/lib/active_record')
-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]