diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index cb3e27838b8747d18abdc3887f8445f0a882dce3..63cd715920c986402a820fa09f5e203056cd1c26 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,9 @@ +* Don't remove join dependencies in `Relation#exists?` + + Fixes #18632 + + *Sean Griffin* + * Invalid values assigned to a JSON column are assumed to be `nil`. Fixes #18629. diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 088bc203b7f48feda2c1b6c840185421526195ee..c83abfba060a1f85a170929f46d460189287d3d3 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -307,7 +307,7 @@ def exists?(conditions = :none) relation = relation.where(conditions) else unless conditions == :none - relation = where(primary_key => conditions) + relation = relation.where(primary_key => conditions) end end diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 9631ea79bed9938eab849b46942a834202223eb4..d272b9b929f2281d0fa2ea3b7fce164ece8cc5a7 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -857,6 +857,12 @@ def test_exists assert ! fake.exists?(authors(:david).id) end + def test_exists_uses_existing_scope + post = authors(:david).posts.first + authors = Author.includes(:posts).where(name: "David", posts: { id: post.id }) + assert authors.exists?(authors(:david).id) + end + def test_last authors = Author.all assert_equal authors(:bob), authors.last