Skip to content

Commit 7bee100

Browse files
committed
Allow to eager load nested nil associations
1 parent 9f9deaf commit 7bee100

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

activerecord/lib/active_record/associations/join_dependency.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def self.walk_tree(associations, hash)
6161
when Hash
6262
associations.each do |k, v|
6363
cache = hash[k] ||= {}
64-
walk_tree v, cache
64+
walk_tree v, cache if v
6565
end
6666
else
6767
raise ConfigurationError, associations.inspect

activerecord/test/cases/associations/cascaded_eager_loading_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ def test_eager_association_loading_with_join_for_count
7777
assert_queries_count(3) { authors.to_a }
7878
end
7979

80+
def test_eager_association_loading_with_nil_associations
81+
authors = Author.includes(nil).to_a
82+
assert_equal 3, authors.size
83+
84+
authors = Author.includes([:posts, nil]).to_a
85+
assert_equal 3, authors.size
86+
87+
authors = Author.includes(posts: nil).to_a
88+
assert_equal 3, authors.size
89+
end
90+
8091
def test_eager_association_loading_with_cascaded_two_levels_with_two_has_many_associations
8192
authors = Author.all.merge!(includes: { posts: [:comments, :categorizations] }, order: "authors.id").to_a
8293
assert_equal 3, authors.size

0 commit comments

Comments
 (0)