Permalink
Browse files

Warn scoped order and limit are ignored. [#4123 state:resolved]

  • Loading branch information...
1 parent 5f7bc47 commit 53ddbfc460f5a54d5b81e7bfbc1c5828f90488c1 @miloops miloops committed Mar 29, 2010
Showing with 18 additions and 4 deletions.
  1. +5 −1 activerecord/lib/active_record/relation/batches.rb
  2. +13 −3 activerecord/test/cases/batches_test.rb
@@ -50,6 +50,10 @@ def find_each(options = {})
def find_in_batches(options = {})
relation = self
+ if orders.present? || taken.present?
+ ActiveRecord::Base.logger.warn("Scoped order and limit are ignored, it's forced to be batch order and batch size")
+ end
+
if (finder_options = options.except(:start, :batch_size)).present?
raise "You can't specify an order, it's forced to be #{batch_order}" if options[:order].present?
raise "You can't specify a limit, it's forced to be the batch_size" if options[:limit].present?
@@ -77,4 +81,4 @@ def batch_order
"#{@klass.table_name}.#{@klass.primary_key} ASC"
end
end
-end
+end
@@ -8,7 +8,7 @@ def setup
@posts = Post.order("id asc")
@total = Post.count
end
-
+
def test_each_should_excecute_one_query_per_batch
assert_queries(Post.count + 1) do
Post.find_each(:batch_size => 1) do |post|
@@ -28,7 +28,17 @@ def test_each_should_raise_if_the_limit_is_set
Post.find_each(:limit => 1) { |post| post }
end
end
-
+
+ def test_warn_if_limit_scope_is_set
+ ActiveRecord::Base.logger.expects(:warn)
+ Post.limit(1).find_each { |post| post }
+ end
+
+ def test_warn_if_order_scope_is_set
+ ActiveRecord::Base.logger.expects(:warn)
+ Post.order("title").find_each { |post| post }
+ end
+
def test_find_in_batches_should_return_batches
assert_queries(Post.count + 1) do
Post.find_in_batches(:batch_size => 1) do |batch|
@@ -58,4 +68,4 @@ def test_find_in_batches_shouldnt_excute_query_unless_needed
Post.find_in_batches(:batch_size => post_count + 1) {|batch| assert_kind_of Array, batch }
end
end
-end
+end

0 comments on commit 53ddbfc

Please sign in to comment.