diff options
author | Ben Woosley <ben.woosley@gmail.com> | 2012-05-28 12:23:37 -0700 |
---|---|---|
committer | Ben Woosley <ben.woosley@gmail.com> | 2013-05-10 16:13:46 +0200 |
commit | 15d6e4dce7126fe24bce5cdb91d2ffee68648420 (patch) | |
tree | dd482d3d61b0b68aba9a085a8bd7929eda45feae /activerecord/lib/active_record/relation | |
parent | 0593c00dfbdab221116e49d3b0a7c57605160fe2 (diff) | |
download | rails-15d6e4dce7126fe24bce5cdb91d2ffee68648420.tar.gz rails-15d6e4dce7126fe24bce5cdb91d2ffee68648420.tar.bz2 rails-15d6e4dce7126fe24bce5cdb91d2ffee68648420.zip |
Fix that #exists? can produce invalid SQL: "SELECT DISTINCT DISTINCT"
The combination of a :uniq => true association and the #distinct call
in #construct_limited_ids_condition combine to create invalid SQL, because
we're explicitly selecting DISTINCT, and also sending #distinct on to AREL,
via the relation#distinct_value.
Rather than build a select distinct clause in #construct_limited_ids_condition,
I set #distinct! and pass just the columns into the select statement.
This requires introducing a #columns_for_distinct method to return the
select columns but not the statement itself.
Diffstat (limited to 'activerecord/lib/active_record/relation')
-rw-r--r-- | activerecord/lib/active_record/relation/finder_methods.rb | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 72e9272cd7..ff825e52c1 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -245,9 +245,9 @@ module ActiveRecord def construct_limited_ids_condition(relation) orders = relation.order_values.map { |val| val.presence }.compact - values = @klass.connection.distinct("#{quoted_table_name}.#{primary_key}", orders) + values = @klass.connection.columns_for_distinct("#{quoted_table_name}.#{quoted_primary_key}", orders) - relation = relation.dup.select(values) + relation = relation.dup.select(values).distinct! id_rows = @klass.connection.select_all(relation.arel, 'SQL', relation.bind_values) ids_array = id_rows.map {|row| row[primary_key]} |