From 53ddbfc460f5a54d5b81e7bfbc1c5828f90488c1 Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Mon, 29 Mar 2010 11:24:35 -0300 Subject: Warn scoped order and limit are ignored. [#4123 state:resolved] --- activerecord/lib/active_record/relation/batches.rb | 6 +++++- activerecord/test/cases/batches_test.rb | 16 +++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/relation/batches.rb b/activerecord/lib/active_record/relation/batches.rb index cb57b445e5..1c61e7d450 100644 --- a/activerecord/lib/active_record/relation/batches.rb +++ b/activerecord/lib/active_record/relation/batches.rb @@ -50,6 +50,10 @@ module ActiveRecord 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 @@ module ActiveRecord "#{@klass.table_name}.#{@klass.primary_key} ASC" end end -end \ No newline at end of file +end diff --git a/activerecord/test/cases/batches_test.rb b/activerecord/test/cases/batches_test.rb index e417d8a803..83deabb5b7 100644 --- a/activerecord/test/cases/batches_test.rb +++ b/activerecord/test/cases/batches_test.rb @@ -8,7 +8,7 @@ class EachTest < ActiveRecord::TestCase @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 @@ class EachTest < ActiveRecord::TestCase 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 @@ class EachTest < ActiveRecord::TestCase Post.find_in_batches(:batch_size => post_count + 1) {|batch| assert_kind_of Array, batch } end end -end \ No newline at end of file +end -- cgit v1.2.3 From 7006aa1a853e90f5883b3dc5913b4e310789bc6f Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Mon, 29 Mar 2010 12:30:13 -0300 Subject: Arel now handles ranges with excluded end. --- activerecord/lib/active_record/relation/predicate_builder.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index 7e83eccbb5..711df16bf1 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -27,12 +27,7 @@ module ActiveRecord values = value.to_a attribute.in(values) when Range - # TODO : Arel should handle ranges with excluded end. - if value.exclude_end? - [attribute.gteq(value.begin), attribute.lt(value.end)] - else - attribute.in(value) - end + attribute.in(value) else attribute.eq(value) end -- cgit v1.2.3