Skip to content

Commit 07af54d

Browse files
authored
Merge pull request rails#26980 from kamipo/respect_new_records_for_collection_proxy_distinct
Respect new records for `CollectionProxy#uniq`
2 parents f48bb1b + 0ec967a commit 07af54d

File tree

6 files changed

+19
-13
lines changed

6 files changed

+19
-13
lines changed

activerecord/lib/active_record/associations/collection_association.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,6 @@ def empty?
246246
end
247247
end
248248

249-
def distinct
250-
seen = {}
251-
load_target.find_all do |record|
252-
seen[record.id] = true unless seen.key?(record.id)
253-
end
254-
end
255-
256249
# Replace this collection with +other_array+. This will perform a diff
257250
# and delete/add only records that have changed.
258251
def replace(other_array)

activerecord/lib/active_record/associations/collection_proxy.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,12 @@ def destroy(*records)
718718
@association.destroy(*records)
719719
end
720720

721+
##
722+
# :method: distinct
723+
#
724+
# :call-seq:
725+
# distinct(value = true)
726+
#
721727
# Specifies whether the records should be unique or not.
722728
#
723729
# class Person < ActiveRecord::Base
@@ -732,10 +738,17 @@ def destroy(*records)
732738
#
733739
# person.pets.select(:name).distinct
734740
# # => [#<Pet name: "Fancy-Fancy">]
735-
def distinct
736-
@association.distinct
741+
#
742+
# person.pets.select(:name).distinct.distinct(false)
743+
# # => [
744+
# # #<Pet name: "Fancy-Fancy">,
745+
# # #<Pet name: "Fancy-Fancy">
746+
# # ]
747+
748+
#--
749+
def uniq
750+
load_target.uniq
737751
end
738-
alias uniq distinct
739752

740753
def calculate(operation, column_name)
741754
null_scope? ? scope.calculate(operation, column_name) : super

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
@@ -383,7 +383,7 @@ def test_distinct_after_the_fact
383383
dev.projects << projects(:active_record)
384384

385385
assert_equal 3, dev.projects.size
386-
assert_equal 1, dev.projects.distinct.size
386+
assert_equal 1, dev.projects.uniq.size
387387
end
388388

389389
def test_distinct_before_the_fact

activerecord/test/cases/associations/has_many_associations_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,7 @@ def test_collection_size_after_building
919919
company.clients_of_firm.build("name" => "Another Client")
920920
company.clients_of_firm.build("name" => "Yet Another Client")
921921
assert_equal 4, company.clients_of_firm.size
922+
assert_equal 4, company.clients_of_firm.uniq.size
922923
end
923924

924925
def test_collection_not_empty_after_building

activerecord/test/cases/associations/join_model_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def test_include_has_many_through_polymorphic_has_many
413413
author = Author.includes(:taggings).find authors(:david).id
414414
expected_taggings = taggings(:welcome_general, :thinking_general)
415415
assert_no_queries do
416-
assert_equal expected_taggings, author.taggings.distinct.sort_by(&:id)
416+
assert_equal expected_taggings, author.taggings.uniq.sort_by(&:id)
417417
end
418418
end
419419

activerecord/test/cases/relations_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,6 @@ def test_dynamic_find_by_attributes
785785
expected_taggings = taggings(:welcome_general, :thinking_general)
786786

787787
assert_no_queries do
788-
assert_equal expected_taggings, author.taggings.distinct.sort_by(&:id)
789788
assert_equal expected_taggings, author.taggings.uniq.sort_by(&:id)
790789
end
791790

0 commit comments

Comments
 (0)