aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorKassio Borges <kassioborgesm@gmail.com>2014-08-18 18:18:34 -0300
committerKassio Borges <kassioborgesm@gmail.com>2014-08-18 18:20:40 -0300
commit7aeca5066ada5077cb6aaf7fb331a146afc76cfd (patch)
treec5a2588e1f1659e441a2e8bce442e2aed5e36eb4 /activerecord
parent986dee5ca1db38ddb2e5394a0cc88831b175e018 (diff)
downloadrails-7aeca5066ada5077cb6aaf7fb331a146afc76cfd.tar.gz
rails-7aeca5066ada5077cb6aaf7fb331a146afc76cfd.tar.bz2
rails-7aeca5066ada5077cb6aaf7fb331a146afc76cfd.zip
Fixes the `Relation#exists?` to work with polymorphic associations.
Fixes #15821.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb2
-rw-r--r--activerecord/test/cases/finder_test.rb14
-rw-r--r--activerecord/test/models/post.rb1
4 files changed, 22 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 0fbe5bfaed..372f4e210f 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+* Fixed the `Relation#exists?` to work with polymorphic associations.
+
+ Fixes #15821.
+
+ *Kassio Borges*
+
* Currently, Active Record will rescue any errors raised within
after_rollback/after_create callbacks and print them to the logs. Next versions of rails
will not rescue those errors anymore, and just bubble them up, as the other callbacks.
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 0c9c761f97..a7899da3a8 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -304,7 +304,7 @@ module ActiveRecord
end
end
- connection.select_value(relation, "#{name} Exists", relation.bind_values) ? true : false
+ connection.select_value(relation, "#{name} Exists", relation.arel.bind_values + relation.bind_values) ? true : false
end
# This method is called whenever no records are found with either a single
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index 95d006279d..b42a60fea5 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -4,6 +4,7 @@ require 'models/author'
require 'models/categorization'
require 'models/comment'
require 'models/company'
+require 'models/tagging'
require 'models/topic'
require 'models/reply'
require 'models/entrant'
@@ -78,6 +79,19 @@ class FinderTest < ActiveRecord::TestCase
assert_raise(NoMethodError) { Topic.exists?([1,2]) }
end
+ def test_exists_with_polymorphic_relation
+ post = Post.create!(title: 'Post', body: 'default', taggings: [Tagging.new(comment: 'tagging comment')])
+ relation = Post.tagged_with_comment('tagging comment')
+
+ assert_equal true, relation.exists?(title: ['Post'])
+ assert_equal true, relation.exists?(['title LIKE ?', 'Post%'])
+ assert_equal true, relation.exists?
+ assert_equal true, relation.exists?(post.id)
+ assert_equal true, relation.exists?(post.id.to_s)
+
+ assert_equal false, relation.exists?(false)
+ end
+
def test_exists_passing_active_record_object_is_deprecated
assert_deprecated do
Topic.exists?(Topic.new)
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index a29858213b..56a31011da 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -41,6 +41,7 @@ class Post < ActiveRecord::Base
scope :with_tags, -> { preload(:taggings) }
scope :tagged_with, ->(id) { joins(:taggings).where(taggings: { tag_id: id }) }
+ scope :tagged_with_comment, ->(comment) { joins(:taggings).where(taggings: { comment: comment }) }
has_many :comments do
def find_most_recent