From 2bf7c30049558c2503f20e9810fdf4727650fe99 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Mon, 7 Nov 2016 00:27:50 +0900 Subject: Should except `:distinct` rather than `:order` for `exists?` Records fetching order is very important for performance if `limit` is presented. Should not except the order in the case. And `exists?` replaces select list to `1 AS one` therefore `:distinct` is useless (`DISTINCT 1 AS one`). And PostgreSQL raises the following error if `:distinct` and `:order` are used in the same time. ``` ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list ``` --- activerecord/lib/active_record/relation/finder_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 97a819c5af..55ded4c6d0 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -321,7 +321,7 @@ module ActiveRecord relation = apply_join_dependency(self, construct_join_dependency(eager_loading: false)) return false if ActiveRecord::NullRelation === relation - relation = relation.except(:select, :order).select(ONE_AS_ONE).limit(1) + relation = relation.except(:select, :distinct).select(ONE_AS_ONE).limit(1) case conditions when Array, Hash -- cgit v1.2.3