Skip to content

Commit a1bb6c8

Browse files
committed
rename Relation#uniq to Relation#distinct. #uniq still works.
The similarity of `Relation#uniq` to `Array#uniq` is confusing. Since our Relation API is close to SQL terms I renamed `#uniq` to `#distinct`. There is no deprecation. `#uniq` and `#uniq!` are aliases and will continue to work. I also updated the documentation to promote the use of `#distinct`.
1 parent bfee706 commit a1bb6c8

22 files changed

+86
-45
lines changed

activerecord/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
## Rails 4.0.0 (unreleased) ##
22

3+
* Rename `Relation#uniq` to `Relation#distinct`. `#uniq` is still
4+
available as an alias but we encourage to use `#distinct` instead.
5+
Also `Relation#uniq_value` is aliased to `Relation#distinct_value`,
6+
this is a temporary solution and you should migrate to `distinct_value`.
7+
8+
*Yves Senn*
9+
310
* Fix quoting for sqlite migrations using copy_table_contents() with binary
411
columns.
512

activerecord/lib/active_record/associations.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ def association_instance_set(name, association)
241241
# others.destroy_all | X | X | X
242242
# others.find(*args) | X | X | X
243243
# others.exists? | X | X | X
244+
# others.distinct | X | X | X
244245
# others.uniq | X | X | X
245246
# others.reset | X | X | X
246247
#

activerecord/lib/active_record/associations/collection_association.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def count(column_name = nil, count_options = {})
174174

175175
reflection.klass.count_by_sql(custom_counter_sql)
176176
else
177-
if association_scope.uniq_value
177+
if association_scope.distinct_value
178178
# This is needed because 'SELECT count(DISTINCT *)..' is not valid SQL.
179179
column_name ||= reflection.klass.primary_key
180180
count_options[:distinct] = true
@@ -246,14 +246,14 @@ def destroy(*records)
246246
# +count_records+, which is a method descendants have to provide.
247247
def size
248248
if !find_target? || loaded?
249-
if association_scope.uniq_value
249+
if association_scope.distinct_value
250250
target.uniq.size
251251
else
252252
target.size
253253
end
254254
elsif !loaded? && !association_scope.group_values.empty?
255255
load_target.size
256-
elsif !loaded? && !association_scope.uniq_value && target.is_a?(Array)
256+
elsif !loaded? && !association_scope.distinct_value && target.is_a?(Array)
257257
unsaved_records = target.select { |r| r.new_record? }
258258
unsaved_records.size + count_records
259259
else
@@ -306,12 +306,13 @@ def many?
306306
end
307307
end
308308

309-
def uniq
309+
def distinct
310310
seen = {}
311311
load_target.find_all do |record|
312312
seen[record.id] = true unless seen.key?(record.id)
313313
end
314314
end
315+
alias uniq distinct
315316

316317
# Replace this collection with +other_array+. This will perform a diff
317318
# and delete/add only records that have changed.
@@ -352,7 +353,7 @@ def add_to_target(record)
352353
callback(:before_add, record)
353354
yield(record) if block_given?
354355

355-
if association_scope.uniq_value && index = @target.index(record)
356+
if association_scope.distinct_value && index = @target.index(record)
356357
@target[index] = record
357358
else
358359
@target << record

activerecord/lib/active_record/associations/collection_proxy.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,11 +649,12 @@ def destroy(*records)
649649
# # #<Pet name: "Fancy-Fancy">
650650
# # ]
651651
#
652-
# person.pets.select(:name).uniq
652+
# person.pets.select(:name).distinct
653653
# # => [#<Pet name: "Fancy-Fancy">]
654-
def uniq
655-
@association.uniq
654+
def distinct
655+
@association.distinct
656656
end
657+
alias uniq distinct
657658

658659
# Count all records using SQL.
659660
#

activerecord/lib/active_record/associations/preloader/has_many_through.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class HasManyThrough < CollectionAssociation #:nodoc:
66

77
def associated_records_by_owner
88
super.each do |owner, records|
9-
records.uniq! if reflection_scope.uniq_value
9+
records.uniq! if reflection_scope.distinct_value
1010
end
1111
end
1212
end

activerecord/lib/active_record/querying.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Querying
88
delegate :find_each, :find_in_batches, :to => :all
99
delegate :select, :group, :order, :except, :reorder, :limit, :offset, :joins,
1010
:where, :preload, :eager_load, :includes, :from, :lock, :readonly,
11-
:having, :create_with, :uniq, :references, :none, :to => :all
11+
:having, :create_with, :uniq, :distinct, :references, :none, :to => :all
1212
delegate :count, :average, :minimum, :maximum, :sum, :calculate, :pluck, :ids, :to => :all
1313

1414
# Executes a custom SQL query against your database and returns all the results. The results will

activerecord/lib/active_record/relation.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Relation
1010
:extending]
1111

1212
SINGLE_VALUE_METHODS = [:limit, :offset, :lock, :readonly, :from, :reordering,
13-
:reverse_order, :uniq, :create_with]
13+
:reverse_order, :distinct, :create_with]
1414

1515
VALUE_METHODS = MULTI_VALUE_METHODS + SINGLE_VALUE_METHODS
1616

@@ -506,6 +506,12 @@ def joined_includes_values
506506
includes_values & joins_values
507507
end
508508

509+
# +uniq+ and +uniq!+ are silently deprecated. +uniq_value+ delegates to +distinct_value+
510+
# to maintain backwards compatibility. Use +distinct_value+ instead.
511+
def uniq_value
512+
distinct_value
513+
end
514+
509515
# Compares two relations for equality.
510516
def ==(other)
511517
case other

activerecord/lib/active_record/relation/calculations.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ def has_include?(column_name)
198198
def perform_calculation(operation, column_name, options = {})
199199
operation = operation.to_s.downcase
200200

201-
# If #count is used in conjuction with #uniq it is considered distinct. (eg. relation.uniq.count)
202-
distinct = options[:distinct] || self.uniq_value
201+
# If #count is used with #distinct / #uniq it is considered distinct. (eg. relation.distinct.count)
202+
distinct = options[:distinct] || self.distinct_value
203203

204204
if operation == "count"
205205
column_name ||= (select_for_count || :all)

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -710,20 +710,22 @@ def from!(value, subquery_name = nil) # :nodoc:
710710
# User.select(:name)
711711
# # => Might return two records with the same name
712712
#
713-
# User.select(:name).uniq
714-
# # => Returns 1 record per unique name
713+
# User.select(:name).distinct
714+
# # => Returns 1 record per distinct name
715715
#
716-
# User.select(:name).uniq.uniq(false)
716+
# User.select(:name).distinct.distinct(false)
717717
# # => You can also remove the uniqueness
718-
def uniq(value = true)
719-
spawn.uniq!(value)
718+
def distinct(value = true)
719+
spawn.distinct!(value)
720720
end
721+
alias uniq distinct
721722

722-
# Like #uniq, but modifies relation in place.
723-
def uniq!(value = true) # :nodoc:
724-
self.uniq_value = value
723+
# Like #distinct, but modifies relation in place.
724+
def distinct!(value = true) # :nodoc:
725+
self.distinct_value = value
725726
self
726727
end
728+
alias uniq! distinct!
727729

728730
# Used to extend a scope with additional methods, either through
729731
# a module or through a block provided.
@@ -814,7 +816,7 @@ def build_arel
814816

815817
build_select(arel, select_values.uniq)
816818

817-
arel.distinct(uniq_value)
819+
arel.distinct(distinct_value)
818820
arel.from(build_from) if from_value
819821
arel.lock(lock_value) if lock_value
820822

activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def test_uniq_after_the_fact
316316
dev.projects << projects(:active_record)
317317

318318
assert_equal 3, dev.projects.size
319-
assert_equal 1, dev.projects.uniq.size
319+
assert_equal 1, dev.projects.distinct.size
320320
end
321321

322322
def test_uniq_before_the_fact

0 commit comments

Comments
 (0)