Skip to content
This repository
Browse code

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 authored josevalim committed
7  activerecord/lib/active_record/batches.rb
@@ -66,7 +66,12 @@ def find_in_batches(options = {})
66 66
           yield records
67 67
 
68 68
           break if records.size < batch_size
69  
-          records = proxy.find(:all, :conditions => [ "#{table_name}.#{primary_key} > ?", records.last.id ])
  69
+          
  70
+          last_value = records.last.id
  71
+          
  72
+          raise "You must include the primary key if you define a select" unless last_value.present?
  73
+          
  74
+          records = proxy.find(:all, :conditions => [ "#{table_name}.#{primary_key} > ?", last_value ])
70 75
         end
71 76
       end
72 77
 
14  activerecord/test/cases/batches_test.rb
@@ -64,4 +64,18 @@ def test_find_in_batches_doesnt_clog_conditions
64 64
       assert_nothing_raised { Post.find(posts(:thinking).id) }
65 65
     end
66 66
   end
  67
+  
  68
+  def test_each_should_raise_if_select_is_set_without_id
  69
+    assert_raise(RuntimeError) do
  70
+      Post.find_each(:select => :title, :batch_size => 1) { |post| post }
  71
+    end
  72
+  end
  73
+
  74
+  def test_each_should_execute_if_id_is_in_select
  75
+    assert_queries(4) do
  76
+      Post.find_each(:select => "id, title, type", :batch_size => 2) do |post|
  77
+        assert_kind_of Post, post
  78
+      end
  79
+    end
  80
+  end
67 81
 end

0 notes on commit 8e7a64d

Please sign in to comment.
Something went wrong with that request. Please try again.