From 100f3a2a29cdea4203260377b08a14af2138c953 Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Tue, 11 May 2010 22:01:37 +0200 Subject: [PATCH] backported AR correction to find_each and find_in_batches to raise when the user uses select but does not specify the primary key --- activerecord/lib/active_record/batches.rb | 7 ++++++- activerecord/test/cases/batches_test.rb | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/batches.rb b/activerecord/lib/active_record/batches.rb index 99c4598..acbbd98 100644 --- a/activerecord/lib/active_record/batches.rb +++ b/activerecord/lib/active_record/batches.rb @@ -66,7 +66,12 @@ module ActiveRecord 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 diff --git a/activerecord/test/cases/batches_test.rb b/activerecord/test/cases/batches_test.rb index 68b9ffd..3bf8bc8 100644 --- a/activerecord/test/cases/batches_test.rb +++ b/activerecord/test/cases/batches_test.rb @@ -64,4 +64,18 @@ class EachTest < ActiveRecord::TestCase 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 \ No newline at end of file -- 1.6.3.1