1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fixes the Relation#exists? to work with polymorphic associations.

Fixes #15821.
This commit is contained in:
Kassio Borges 2014-08-18 18:18:34 -03:00
parent 986dee5ca1
commit 7aeca5066a
4 changed files with 22 additions and 1 deletions

View file

@ -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 * Currently, Active Record will rescue any errors raised within
after_rollback/after_create callbacks and print them to the logs. Next versions of rails 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. will not rescue those errors anymore, and just bubble them up, as the other callbacks.

View file

@ -304,7 +304,7 @@ module ActiveRecord
end end
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 end
# This method is called whenever no records are found with either a single # This method is called whenever no records are found with either a single

View file

@ -4,6 +4,7 @@ require 'models/author'
require 'models/categorization' require 'models/categorization'
require 'models/comment' require 'models/comment'
require 'models/company' require 'models/company'
require 'models/tagging'
require 'models/topic' require 'models/topic'
require 'models/reply' require 'models/reply'
require 'models/entrant' require 'models/entrant'
@ -78,6 +79,19 @@ class FinderTest < ActiveRecord::TestCase
assert_raise(NoMethodError) { Topic.exists?([1,2]) } assert_raise(NoMethodError) { Topic.exists?([1,2]) }
end 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 def test_exists_passing_active_record_object_is_deprecated
assert_deprecated do assert_deprecated do
Topic.exists?(Topic.new) Topic.exists?(Topic.new)

View file

@ -41,6 +41,7 @@ class Post < ActiveRecord::Base
scope :with_tags, -> { preload(:taggings) } scope :with_tags, -> { preload(:taggings) }
scope :tagged_with, ->(id) { joins(:taggings).where(taggings: { tag_id: id }) } 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 has_many :comments do
def find_most_recent def find_most_recent