Skip to content

Commit 9f52c45

Browse files
authored
Merge pull request rails#50308 from fatkodima/refactor-loaded-batches
Small refactoring of batching over loaded relation
2 parents 3064d4f + 5114863 commit 9f52c45

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

activerecord/lib/active_record/relation/batches.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,15 @@ def batch_on_loaded_relation(relation:, start:, finish:, order:, batch_limit:)
345345
end
346346
end
347347

348-
records = records.sort_by { |record| record.id }
348+
records.sort_by!(&:id)
349349

350350
if order == :desc
351351
records.reverse!
352352
end
353353

354-
(0...records.size).step(batch_limit).each do |start|
354+
records.each_slice(batch_limit) do |subrecords|
355355
subrelation = relation.spawn
356-
subrelation.load_records(records[start, batch_limit])
356+
subrelation.load_records(subrecords)
357357

358358
yield subrelation
359359
end

0 commit comments

Comments
 (0)