Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

`#count` in conjunction with `#uniq` performs distinct count. #9078

Merged
merged 1 commit into from

4 participants

@senny
Owner

This is a fix for #6865

It makes sure that we perform a COUNT(DISTINCT *) when Relation.uniq.count is used. While writing up the patch we noticed that the "count" code path is very different from the other calculations. I wanted to submit a small patch for the problem but we will try to clean up the count code in a second PR.

@senny
Owner

@rafaelfranca @jonleighton could you take a look? Also what do you think about a backport? Too risky?

activerecord/lib/active_record/relation/calculations.rb
((7 lines not shown))
if operation == "count"
column_name ||= (select_for_count || :all)
-
@rafaelfranca Owner

Please don't remove this line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@carlosantoniodasilva

Seems good.

@luke-gru

Ran into this issue just the other day. Thanks!

@senny
Owner

@rafaelfranca @carlosantoniodasilva changes are made. Could you check again?

@rafaelfranca rafaelfranca merged commit 647d18c into rails:master
@senny senny deleted the senny:6865_ar_count_with_uniq branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 26, 2013
  1. @senny
This page is out of date. Refresh to see the latest.
View
9 activerecord/CHANGELOG.md
@@ -1,5 +1,14 @@
## Rails 4.0.0 (unreleased) ##
+* When `#count` is used in conjunction with `#uniq` we perform `count(:distinct => true)`.
+ Fix #6865.
+
+ Example:
+
+ relation.uniq.count # => SELECT COUNT(DISTINCT *)
+
+ *Yves Senn + Kaspar Schiess*
+
* PostgreSQL ranges type support. Includes: int4range, int8range,
numrange, tsrange, tstzrange, daterange
View
3  activerecord/lib/active_record/relation/calculations.rb
@@ -192,7 +192,8 @@ def has_include?(column_name)
def perform_calculation(operation, column_name, options = {})
operation = operation.to_s.downcase
- distinct = options[:distinct]
+ # If #count is used in conjuction with #uniq it is considered distinct. (eg. relation.uniq.count)
+ distinct = options[:distinct] || self.uniq_value
if operation == "count"
column_name ||= (select_for_count || :all)
View
4 activerecord/test/cases/calculations_test.rb
@@ -341,6 +341,10 @@ def test_count_with_column_parameter
assert_equal 5, Account.count(:firm_id)
end
+ def test_count_with_uniq
+ assert_equal 4, Account.select(:credit_limit).uniq.count
+ end
+
def test_count_with_column_and_options_parameter
assert_equal 2, Account.where("credit_limit = 50 AND firm_id IS NOT NULL").count(:firm_id)
end
Something went wrong with that request. Please try again.