dr. strange
dr. strange

Reputation: 665

how to trigger ActiveRecord callbacks (has_paper_trail) for update_all in rails

when i update all the authors papertrail won't create record in version table b'coz update_all wan't trigger ActiveRecord callbacks

Billing.update_all( "author = 'David'", "title LIKE '%Rails%'" )

Upvotes: 3

Views: 2584

Answers (1)

RKelley
RKelley

Reputation: 558

ActiveRecord update_all does not instantiate the objects and does not trigger callbacks. As of version 6.0.2 Airblade still will not insert new versions in the versions table when using update_all or batch updates. See issue #337

If you're dealing with a small amount of data at a time, you can trigger callbacks by calling update on the relation in Rails 5 docs.

Billing.where("title LIKE '%Rails%'").update(author: 'David')

If you're using a previous version of Rails, you'll need to loop through the collection of objects and call update on each instance.

Upvotes: 3

Related Questions