How can I run updates in batches in Rails 3/4?

后端 未结 6 1151
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-05 01:09

I need to mass-update many thousands of records, and I would like to process the updates in batches. First, I tried:

Foo.where(bar: \'bar\').find_in_batches.upda         


        
6条回答
  •  半阙折子戏
    2021-02-05 02:12

    In Rails 5, there's a new handy method ActiveRecord::Relation#in_batches to solve this problem:

    Foo.in_batches.update_all(bar: 'baz')
    

    Check documentation for details.

提交回复
热议问题