From 82bfe3bf115e7d4d6f5f5b40f8ab32a61148da67 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sat, 2 Sep 2017 07:17:56 +0900 Subject: Add a test case for preloading through association with implicit source If `reflection_scope.where_clause` is not empty, `through_scope` should be joined the source association. But if the through association doesn't have explicit `:source`, `options[:source]` will be nil and `scope.includes_values` will also be empty. It should use `source_reflection.name` rather than `options[:source]`. Fixed by a26cff3c1235c61cd0135bed4ef63d7be452b458. Fixes #11078. Fixes #26129. Closes #14312. Closes #29155. Closes #29841. --- activerecord/test/cases/associations/eager_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 44bf7f7d2f..9fb395853e 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -530,6 +530,14 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_equal [comments(:does_it_hurt)], assert_no_queries { author.special_post_comments } end + def test_preloading_has_many_through_with_implicit_source + authors = Author.includes(:very_special_comments).to_a + assert_no_queries do + special_comment_authors = authors.map { |author| [author.name, author.very_special_comments.size] } + assert_equal [["David", 1], ["Mary", 0], ["Bob", 0]], special_comment_authors + end + end + def test_eager_with_has_many_through_an_sti_join_model_with_conditions_on_both author = Author.all.merge!(includes: :special_nonexistent_post_comments, order: "authors.id").first assert_equal [], author.special_nonexistent_post_comments -- cgit v1.2.3