Stop find_in_batches using the records after yielding. #1435
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently if the code that calls
find_in_batches
modifies the yielded array in place thenfind_in_batches
can enter an infinite loop searching with ruby object ids in the database instead of the primary key of records in the database. This happens because it naively assumes the yielded array hasn't been modified before callingid
on the last object in the array. And ruby (1.8 at least) alias'id
toobject_id
so an integer is still returned no matter what the last object is.By moving finding the
id
of the last object before yielding the array of records it means the calling code can do whatever it wants to the array in terms of modifying it in place, andfind_in_batches
doesn't care. Also moved the count of the array records before yielding in case elements are removed or added when yielded.