mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
parent
01341e3e43
commit
648def4060
3 changed files with 15 additions and 1 deletions
|
@ -1,5 +1,14 @@
|
||||||
## Rails 4.0.0 (unreleased) ##
|
## 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,
|
* PostgreSQL ranges type support. Includes: int4range, int8range,
|
||||||
numrange, tsrange, tstzrange, daterange
|
numrange, tsrange, tstzrange, daterange
|
||||||
|
|
||||||
|
|
|
@ -192,7 +192,8 @@ module ActiveRecord
|
||||||
def perform_calculation(operation, column_name, options = {})
|
def perform_calculation(operation, column_name, options = {})
|
||||||
operation = operation.to_s.downcase
|
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"
|
if operation == "count"
|
||||||
column_name ||= (select_for_count || :all)
|
column_name ||= (select_for_count || :all)
|
||||||
|
|
|
@ -341,6 +341,10 @@ class CalculationsTest < ActiveRecord::TestCase
|
||||||
assert_equal 5, Account.count(:firm_id)
|
assert_equal 5, Account.count(:firm_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_count_with_uniq
|
||||||
|
assert_equal 4, Account.select(:credit_limit).uniq.count
|
||||||
|
end
|
||||||
|
|
||||||
def test_count_with_column_and_options_parameter
|
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)
|
assert_equal 2, Account.where("credit_limit = 50 AND firm_id IS NOT NULL").count(:firm_id)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue