aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2019-02-26 18:07:13 -0500
committerRafael Mendonça França <rafaelmfranca@gmail.com>2019-02-26 18:07:13 -0500
commit572dcdd7e858f126848bdf4f2be0f5cb0de7c026 (patch)
treeb0bad61734f2ded9782bf51cd701bffb1a6e56d9 /activerecord/lib/active_record/associations
parent196e22de5378cf684760f082c8131f728f4717c4 (diff)
downloadrails-572dcdd7e858f126848bdf4f2be0f5cb0de7c026.tar.gz
rails-572dcdd7e858f126848bdf4f2be0f5cb0de7c026.tar.bz2
rails-572dcdd7e858f126848bdf4f2be0f5cb0de7c026.zip
Fix preload with nested associations
When the middle association doesn't have any records and the inner association is not an empty scope the owner will be `nil` so we can't try to reset the inverse association.
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r--activerecord/lib/active_record/associations/preloader/through_association.rb6
1 files changed, 5 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/preloader/through_association.rb b/activerecord/lib/active_record/associations/preloader/through_association.rb
index 25254e652a..32653956b2 100644
--- a/activerecord/lib/active_record/associations/preloader/through_association.rb
+++ b/activerecord/lib/active_record/associations/preloader/through_association.rb
@@ -14,6 +14,7 @@ module ActiveRecord
owners.each do |owner|
through_records = Array(owner.association(through_reflection.name).target)
+
if already_loaded
if source_type = reflection.options[:source_type]
through_records = through_records.select do |record|
@@ -23,17 +24,20 @@ module ActiveRecord
else
owner.association(through_reflection.name).reset if through_scope
end
+
result = through_records.flat_map do |record|
record.association(source_reflection.name).target
end
+
result.compact!
result.sort_by! { |rhs| preload_index[rhs] } if scope.order_values.any?
result.uniq! if scope.distinct_value
associate_records_to_owner(owner, result)
end
+
unless scope.empty_scope?
middle_records.each do |owner|
- owner.association(source_reflection.name).reset
+ owner.association(source_reflection.name).reset if owner
end
end
end