Skip to content

Commit 9effe3c

Browse files
committed
Remove duplicated logic.
1 parent 009aa88 commit 9effe3c

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

activerecord/lib/active_record/associations.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,20 +1805,15 @@ def remove_duplicate_results!(base, records, associations)
18051805
case associations
18061806
when Symbol, String
18071807
reflection = base.reflections[associations]
1808-
if reflection && reflection.collection?
1809-
records.each { |record| record.send(reflection.name).target.uniq! }
1810-
end
1808+
remove_uniq_by_reflection(reflection, records)
18111809
when Array
18121810
associations.each do |association|
18131811
remove_duplicate_results!(base, records, association)
18141812
end
18151813
when Hash
18161814
associations.keys.each do |name|
18171815
reflection = base.reflections[name]
1818-
1819-
if records.any? && reflection.options && reflection.options[:uniq]
1820-
records.each { |record| record.send(reflection.name).target.uniq! }
1821-
end
1816+
remove_uniq_by_reflection(reflection, records)
18221817

18231818
parent_records = []
18241819
records.each do |record|
@@ -1837,6 +1832,7 @@ def remove_duplicate_results!(base, records, associations)
18371832
end
18381833

18391834
protected
1835+
18401836
def build(associations, parent = nil, join_class = Arel::InnerJoin)
18411837
parent ||= @joins.last
18421838
case associations
@@ -1859,6 +1855,12 @@ def build(associations, parent = nil, join_class = Arel::InnerJoin)
18591855
end
18601856
end
18611857

1858+
def remove_uniq_by_reflection(reflection, records)
1859+
if reflection && reflection.collection?
1860+
records.each { |record| record.send(reflection.name).target.uniq! }
1861+
end
1862+
end
1863+
18621864
def build_join_association(reflection, parent)
18631865
JoinAssociation.new(reflection, self, parent)
18641866
end

0 commit comments

Comments
 (0)