diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-01-21 05:31:27 -0800 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-01-21 05:31:27 -0800 |
commit | 6a8a7f89aee25042b795fbfc8689a6db05549c7d (patch) | |
tree | 2d237b2dd3d60768dd791d079a885403e5cdedfb | |
parent | 9322e071e48a6f2ee00de736d7630f71f4dfcb31 (diff) | |
parent | 691709dd6741757e5c4459c8942857ee019b68a0 (diff) | |
download | rails-6a8a7f89aee25042b795fbfc8689a6db05549c7d.tar.gz rails-6a8a7f89aee25042b795fbfc8689a6db05549c7d.tar.bz2 rails-6a8a7f89aee25042b795fbfc8689a6db05549c7d.zip |
Merge pull request #9969 from divineforest/fix-find-in-batches
Fail early with "Primary key not included in the custom select clause" i...
-rw-r--r-- | activerecord/CHANGELOG.md | 8 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/batches.rb | 7 | ||||
-rw-r--r-- | activerecord/test/cases/batches_test.rb | 4 |
3 files changed, 13 insertions, 6 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 170818cd1c..3ca689e713 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,11 @@ +* Fail early with "Primary key not included in the custom select clause" + in find_in_batches. + + Before this patch find_in_batches raises this error only on second iteration. + So you will know about the problem only when you get the batch size threshold. + + *Alexander Balashov* + * Ensure `second` through `fifth` methods act like the `first` finder. The famous ordinal Array instance methods defined in ActiveSupport diff --git a/activerecord/lib/active_record/relation/batches.rb b/activerecord/lib/active_record/relation/batches.rb index 49b01909c6..f02e2365f7 100644 --- a/activerecord/lib/active_record/relation/batches.rb +++ b/activerecord/lib/active_record/relation/batches.rb @@ -102,16 +102,13 @@ module ActiveRecord while records.any? records_size = records.size primary_key_offset = records.last.id + raise "Primary key not included in the custom select clause" unless primary_key_offset yield records break if records_size < batch_size - if primary_key_offset - records = relation.where(table[primary_key].gt(primary_key_offset)).to_a - else - raise "Primary key not included in the custom select clause" - end + records = relation.where(table[primary_key].gt(primary_key_offset)).to_a end end diff --git a/activerecord/test/cases/batches_test.rb b/activerecord/test/cases/batches_test.rb index 38c2560d69..ebb36e4940 100644 --- a/activerecord/test/cases/batches_test.rb +++ b/activerecord/test/cases/batches_test.rb @@ -46,7 +46,9 @@ class EachTest < ActiveRecord::TestCase def test_each_should_raise_if_select_is_set_without_id assert_raise(RuntimeError) do - Post.select(:title).find_each(:batch_size => 1) { |post| post } + Post.select(:title).find_each(batch_size: 1) { |post| + flunk "should not call this block" + } end end |