Permalink
Browse files

Merge pull request #9969 from divineforest/fix-find-in-batches

Fail early with "Primary key not included in the custom select clause" i...
Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
1 parent c4083cb commit df5a87368a1515afb5d19fcc0a6441cf6dfbb428 @senny senny committed Jan 21, 2014
@@ -1,3 +1,12 @@
+* Fail early with "Primary key not included in the custom select clause"
+ in `find_in_batches`.
+
+ Before this patch, the exception was raised after the first batch was
+ yielded to the block. This means that you only get it, when you hit the
+ `batch_size` treshold. This could shadow the issue in development.
+
+ *Alexander Balashov*
+
* ActiveRecord states are now correctly restored after a rollback for
models that did not define any transactional callbacks (i.e.
`after_commit`, `after_rollback` or `after_create`).
@@ -71,16 +71,13 @@ def find_in_batches(options = {})
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
@@ -28,7 +28,9 @@ def test_each_should_not_return_query_chain_and_execute_only_one_query
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

0 comments on commit df5a873

Please sign in to comment.