Skip to content
Browse files

Merge pull request #3572 from amatsuda/ar_exists_uniq_order

exclude ORDER BY clause when querying Relation#exists?
  • Loading branch information...
2 parents 5b21bfa + 74c6e80 commit 6e87281b5f9f6f7abf0ef93448c63396419f1920 @tenderlove tenderlove committed
View
2 activerecord/lib/active_record/relation/finder_methods.rb
@@ -191,7 +191,7 @@ def exists?(id = false)
join_dependency = construct_join_dependency_for_association_find
relation = construct_relation_for_association_find(join_dependency)
- relation = relation.except(:select).select("1").limit(1)
+ relation = relation.except(:select, :order).select("1").limit(1)
case id
when Array, Hash
View
5 activerecord/test/cases/finder_test.rb
@@ -57,6 +57,11 @@ def test_exists_with_nil_arg
assert Topic.first.replies.exists?
end
+ # ensures +exists?+ runs valid SQL by excluding order value
+ def test_exists_with_order
+ assert Topic.order(:id).uniq.exists?
+ end
+
def test_does_not_exist_with_empty_table_and_no_args_given
Topic.delete_all
assert !Topic.exists?

0 comments on commit 6e87281

Please sign in to comment.
Something went wrong with that request. Please try again.