Skip to content

Commit

Permalink
Warn scoped order and limit are ignored. [#4123 state:resolved]
Browse files Browse the repository at this point in the history
  • Loading branch information
miloops committed Mar 29, 2010
1 parent 5f7bc47 commit 53ddbfc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
6 changes: 5 additions & 1 deletion activerecord/lib/active_record/relation/batches.rb
Expand Up @@ -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?
Expand Down Expand Up @@ -77,4 +81,4 @@ def batch_order
"#{@klass.table_name}.#{@klass.primary_key} ASC"
end
end
end
end
16 changes: 13 additions & 3 deletions activerecord/test/cases/batches_test.rb
Expand Up @@ -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|
Expand All @@ -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|
Expand Down Expand Up @@ -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.