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

后端 未结 6 1079
佛祖请我去吃肉
佛祖请我去吃肉 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:02

    Haven't had a chance to test this yet but you might be able to use ARel and a sub query.

    Foo.where(bar: 'bar').select('id').find_in_batches do |foos|
      Foo.where( Foo.arel_table[ :id ].in( foos.to_arel ) ).update_all(bar: 'baz')
    end
    

提交回复
热议问题