diff --git a/lib/core_extensions/activerecord/find_each_ordered.rb b/lib/core_extensions/activerecord/find_each_ordered.rb index d1b783e..c69d69b 100644 --- a/lib/core_extensions/activerecord/find_each_ordered.rb +++ b/lib/core_extensions/activerecord/find_each_ordered.rb @@ -10,8 +10,9 @@ module ActiveRecord limit=arel.limit if limit && (limit < batch_size) then batch_size = limit end records = self.offset(offset).limit(batch_size).all - while records.any? - offset += records.size + records_count = records.count + while records_count > 0 + offset += records_count records.each { |r| yield r } if limit then limit -= records.size @@ -21,6 +22,7 @@ module ActiveRecord end end records = self.offset(offset).limit(batch_size).all + records_count = records.count end end end