Skip to content

Commit c626796

Browse files
committed
Fix swallowing ignore order warning when batching using BatchEnumerator
1 parent fc1a432 commit c626796

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

activerecord/lib/active_record/relation/batches.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,14 @@ def in_batches(of: 1000, start: nil, finish: nil, load: false, error_on_ignore:
260260
cursor = Array(cursor).map(&:to_s)
261261
ensure_valid_options_for_batching!(cursor, start, finish, order)
262262

263-
unless block
264-
return BatchEnumerator.new(of: of, start: start, finish: finish, relation: self, cursor: cursor, order: order, use_ranges: use_ranges)
265-
end
266-
267263
if arel.orders.present?
268264
act_on_ignored_order(error_on_ignore)
269265
end
270266

267+
unless block
268+
return BatchEnumerator.new(of: of, start: start, finish: finish, relation: self, cursor: cursor, order: order, use_ranges: use_ranges)
269+
end
270+
271271
batch_limit = of
272272

273273
if limit_value

activerecord/test/cases/batches_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,16 @@ def test_in_batches_should_not_execute_any_query
318318
end
319319
end
320320

321+
def test_in_batches_should_error_on_ignore_the_order
322+
assert_raise(ArgumentError, match: "Scoped order is ignored") do
323+
PostWithDefaultScope.in_batches(error_on_ignore: true) { }
324+
end
325+
326+
assert_raise(ArgumentError, match: "Scoped order is ignored") do
327+
PostWithDefaultScope.in_batches(error_on_ignore: true).delete_all
328+
end
329+
end
330+
321331
def test_in_batches_has_attribute_readers
322332
enumerator = Post.no_comments.in_batches(of: 2, start: 42, finish: 84)
323333
assert_equal Post.no_comments, enumerator.relation

0 commit comments

Comments
 (0)