Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

define the API

  • Loading branch information...
commit 181b15e0b604300fff799488ae47e3f388783bf3 1 parent f4db87e
@senny authored
View
7 activerecord/lib/active_record/relation/calculations.rb
@@ -11,8 +11,11 @@ module Calculations
# Person.count(:all)
# # => performs a COUNT(*) (:all is an alias for '*')
#
- # Person.count(:age, distinct: true)
- # # => counts the number of different age values
+ # Person.uniq.count(:age)
+ # # => counts the number of different (SQL distinct) age values
+ #
+ # Person.select('DISTINCT age').count
+ # # => counts the number of different (SQL distinct) age values, using raw SQL.
def count(column_name = nil, options = {})
column_name, options = nil, column_name if column_name.is_a?(Hash)
calculate(:count, column_name, options)
View
6 activerecord/test/cases/calculations_test.rb
@@ -355,7 +355,7 @@ def test_should_count_field_in_joined_table
end
def test_should_count_field_in_joined_table_with_group_by
- c = Account.all.merge!(:group => 'accounts.firm_id', :joins => :firm).count('companies.id')
+ c = Account.group('accounts.firm_id').joins(:firm).count('companies.id')
[1,6,2,9].each { |firm_id| assert c.keys.include?(firm_id) }
end
@@ -457,6 +457,10 @@ def test_distinct_is_honored_when_used_with_count_operation_after_group
# Count the number of distinct authors for approved Topics
distinct_authors_for_approved_count = Topic.group(:approved).count(:author_name, :distinct => true)[true]
assert_equal distinct_authors_for_approved_count, 2
+
+ # use #count with #uniq
+ uniq_authors_for_approved_count = Topic.group(:approved).uniq.count(:author_name)[true]
+ assert_equal uniq_authors_for_approved_count, 2
end
def test_pluck
Please sign in to comment.
Something went wrong with that request. Please try again.