Merge pull request #52619 from fatkodima/eager-load-nil-associations

Allow to eager load nested nil associations
This commit is contained in:
Jean Boussier 2024-08-15 22:40:36 +02:00 committed by GitHub
commit d102960369
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 1 deletions

View File

@ -61,7 +61,7 @@ module ActiveRecord
when Hash
associations.each do |k, v|
cache = hash[k] ||= {}
walk_tree v, cache
walk_tree v, cache if v
end
else
raise ConfigurationError, associations.inspect

View File

@ -77,6 +77,17 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
assert_queries_count(3) { authors.to_a }
end
def test_eager_association_loading_with_nil_associations
authors = Author.includes(nil).to_a
assert_equal 3, authors.size
authors = Author.includes([:posts, nil]).to_a
assert_equal 3, authors.size
authors = Author.includes(posts: nil).to_a
assert_equal 3, authors.size
end
def test_eager_association_loading_with_cascaded_two_levels_with_two_has_many_associations
authors = Author.all.merge!(includes: { posts: [:comments, :categorizations] }, order: "authors.id").to_a
assert_equal 3, authors.size