aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-10-02 06:39:42 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-10-09 09:45:32 +0900
commit723b526158df576f673c1bc28439744378a03f5d (patch)
tree664a936a632fe32a48e70c3f0767aa550a02d537 /activerecord
parentf8337575ac1be1efb2bc89225a369e19fa2a234d (diff)
downloadrails-723b526158df576f673c1bc28439744378a03f5d.tar.gz
rails-723b526158df576f673c1bc28439744378a03f5d.tar.bz2
rails-723b526158df576f673c1bc28439744378a03f5d.zip
Fix `relation.exists?` with has_many through associations
`relation.exists?` should reference correct aliases while joining tables of has_many through associations.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb8
-rw-r--r--activerecord/test/cases/finder_test.rb8
2 files changed, 12 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 5ccfe3d1b7..6e07402979 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -366,7 +366,7 @@ module ActiveRecord
# preexisting join in joins_values to categorizations (by way of
# the `has_many :through` for categories).
#
- join_dependency = construct_join_dependency(joins_values)
+ join_dependency = construct_join_dependency
relation = apply_join_dependency(join_dependency)
relation._select!(join_dependency.aliases.columns)
@@ -387,15 +387,15 @@ module ActiveRecord
relation
end
- def construct_join_dependency(joins = [], eager_loading: true)
+ def construct_join_dependency(eager_loading: true)
including = eager_load_values + includes_values
ActiveRecord::Associations::JoinDependency.new(
- klass, table, including, alias_tracker(joins), eager_loading: eager_loading
+ klass, table, including, alias_tracker(joins_values), eager_loading: eager_loading
)
end
def construct_relation_for_association_calculations
- apply_join_dependency(construct_join_dependency(joins_values))
+ apply_join_dependency(construct_join_dependency)
end
def apply_join_dependency(join_dependency)
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index 55496147c1..2f6d708092 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -9,6 +9,7 @@ require "models/company"
require "models/tagging"
require "models/topic"
require "models/reply"
+require "models/rating"
require "models/entrant"
require "models/project"
require "models/developer"
@@ -244,6 +245,13 @@ class FinderTest < ActiveRecord::TestCase
assert_equal true, author.unique_categorized_posts.includes(:special_comments).order("comments.tags_count DESC").limit(1).exists?
end
+ def test_exists_should_reference_correct_aliases_while_joining_tables_of_has_many_through_association
+ assert_nothing_raised do
+ developer = developers(:david)
+ developer.ratings.includes(comment: :post).where(posts: { id: 1 }).exists?
+ end
+ end
+
def test_exists_with_empty_table_and_no_args_given
Topic.delete_all
assert_equal false, Topic.exists?