Skip to content
This repository
Browse code

We need to recorder here. Need to drop the order from default scope.

Fixes #2832
  • Loading branch information...
commit 9066e3402548d365dc7cf2abda8e7cd64370675c 1 parent e865d12
Arun Agrawal authored September 05, 2011
2  activerecord/lib/active_record/relation/batches.rb
@@ -62,7 +62,7 @@ def find_in_batches(options = {})
62 62
       start = options.delete(:start).to_i
63 63
       batch_size = options.delete(:batch_size) || 1000
64 64
 
65  
-      relation = relation.except(:order).order(batch_order).limit(batch_size)
  65
+      relation = relation.reorder(batch_order).limit(batch_size)
66 66
       records = relation.where(table[primary_key].gteq(start)).all
67 67
 
68 68
       while records.any?
20  activerecord/test/cases/batches_test.rb
@@ -113,7 +113,27 @@ def test_find_in_batches_should_not_use_records_after_yielding_them_in_case_orig
113 113
         batch.map! { not_a_post }
114 114
       end
115 115
     end
  116
+  end
116 117
 
  118
+  def test_find_in_batches_should_ignore_the_order_default_scope
  119
+    # First post is with title scope
  120
+    first_post = PostWithDefaultScope.first
  121
+    posts = []
  122
+    PostWithDefaultScope.find_in_batches  do |batch|
  123
+      posts.concat(batch)
  124
+    end
  125
+    # posts.first will be ordered using id only. Title order scope should not apply here
  126
+    assert_not_equal first_post, posts.first
  127
+    assert_equal posts(:welcome), posts.first
  128
+  end
  129
+
  130
+  def test_find_in_batches_should_not_ignore_the_default_scope_if_it_is_other_then_order
  131
+    special_posts_ids = SpecialPostWithDefaultScope.all.map(&:id)
  132
+    posts = []
  133
+    SpecialPostWithDefaultScope.find_in_batches do |batch|
  134
+      posts.concat(batch)
  135
+    end
  136
+    assert_equal special_posts_ids, posts.map(&:id)
117 137
   end
118 138
 
119 139
 end
10  activerecord/test/models/post.rb
@@ -171,4 +171,14 @@ class PostWithDefaultInclude < ActiveRecord::Base
171 171
   self.table_name = 'posts'
172 172
   default_scope includes(:comments)
173 173
   has_many :comments, :foreign_key => :post_id
  174
+end
  175
+
  176
+class PostWithDefaultScope < ActiveRecord::Base
  177
+  self.table_name = 'posts'
  178
+  default_scope :order => :title
  179
+end
  180
+
  181
+class SpecialPostWithDefaultScope < ActiveRecord::Base
  182
+  self.table_name = 'posts'
  183
+  default_scope where(:id => [1, 5,6])
174 184
 end

2 notes on commit 9066e34

Vijay Dev
Collaborator

we need to reorder :-)

Arun Agrawal
Collaborator

@vijaydev Shit... I miss spelled it. Sorry about that.

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