Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

backported AR correction to find_each and find_in_batches to raise wh…

…en the user uses select but does not specify the primary key

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
commit 8e7a64d09003720b333932f86f21eafdf99193bd 1 parent 6949d6f
Josh Kalderimis joshk authored josevalim committed
7 activerecord/lib/active_record/batches.rb
View
@@ -66,7 +66,12 @@ def find_in_batches(options = {})
yield records
break if records.size < batch_size
- records = proxy.find(:all, :conditions => [ "#{table_name}.#{primary_key} > ?", records.last.id ])
+
+ last_value = records.last.id
+
+ raise "You must include the primary key if you define a select" unless last_value.present?
+
+ records = proxy.find(:all, :conditions => [ "#{table_name}.#{primary_key} > ?", last_value ])
end
end
14 activerecord/test/cases/batches_test.rb
View
@@ -64,4 +64,18 @@ def test_find_in_batches_doesnt_clog_conditions
assert_nothing_raised { Post.find(posts(:thinking).id) }
end
end
+
+ def test_each_should_raise_if_select_is_set_without_id
+ assert_raise(RuntimeError) do
+ Post.find_each(:select => :title, :batch_size => 1) { |post| post }
+ end
+ end
+
+ def test_each_should_execute_if_id_is_in_select
+ assert_queries(4) do
+ Post.find_each(:select => "id, title, type", :batch_size => 2) do |post|
+ assert_kind_of Post, post
+ end
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.