我可以使用Rails find_each方法,如:
User.find_each(:batch_size => 10000) do |user| ------ end
用find_each方法有没有办法得到数组的索引?喜欢 :
User.find_each(:batch_size => 10000).with_index do |user,index| ------ end
解决方法
从
method definition可以看出,这是不可能的.
def find_each(options = {}) find_in_batches(options) do |records| records.each { |record| yield record } end end
class User def find_each(options = {}) find_in_batches(options) do |records| records.each_with_index { |record| yield record,index } end end end User.find_each(:batch_size => 10000) do |user,index| ------ end
或使用实例变量.
index = 0 User.find_each(:batch_size => 10000) do |user| # ... index += 1 end