aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/association_preload.rb2
-rw-r--r--activerecord/test/cases/associations/eager_test.rb6
3 files changed, 9 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index a8909b730f..65cf4385c3 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Ensure association preloading doesn't break when an association returns nil. ##11145 [GMFlash]
+
* Make dynamic finders respect the :include on HasManyThrough associations. #10998. [cpytel]
* Base#instantiate_time_object only uses Time.zone when Base.time_zone_aware_attributes is true; leverages Time#time_with_datetime_fallback for readability [Geoff Buesing]
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb
index 87b087bfea..9ef8ed15e8 100644
--- a/activerecord/lib/active_record/association_preload.rb
+++ b/activerecord/lib/active_record/association_preload.rb
@@ -21,7 +21,7 @@ module ActiveRecord
preload_associations(records, parent, preload_options)
reflection = reflections[parent]
parents = records.map {|record| record.send(reflection.name)}.flatten
- unless parents.empty?
+ unless parents.empty? || parents.first.nil?
parents.first.class.preload_associations(parents, child)
end
end
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index 810336684c..1ea101f3c9 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -76,6 +76,12 @@ class EagerAssociationTest < ActiveRecord::TestCase
assert_nil Post.find(posts(:authorless).id, :include => :author).author
end
+ def test_nested_loading_with_no_associations
+ assert_nothing_raised do
+ Post.find(posts(:authorless).id, :include => {:author => :author_addresss})
+ end
+ end
+
def test_eager_association_loading_with_belongs_to_and_foreign_keys
pets = Pet.find(:all, :include => :owner)
assert_equal 3, pets.length