1

I am new to rails. I want to track the versions of my record using paper_trail gem but I am not able to do so when I use the update_columns method. I think overriding the update_columns method may help me but I don't know how exactly should I write the code?

Screw Ball
  • 11
  • 1

1 Answers1

1

How to skip all callbacks except paper-trail when updating an ActiveRecord model using update_column method? .. I use the update_columns method.

Per the Rails docs, the update_columns method skips callbacks. This includes the callbacks installed by PT.

I am new to rails. ... I think overriding the update_columns method may help me ..?

If you're new to rails, you should not override core rails methods.

Solutions

In descending order of preference:

  1. Normally, I'd recommend using update instead of update_columns. But, you want to skip a proper subset of callbacks.
  2. You can touch the record, and PT will record a Version in which only updated_at changed. If you only need object and not object_changes, this is sufficient.
  3. You can use PaperTrail::Version.create just like any other model. This is public API, but you're responsible for passing the correct attributes.
  4. You can use PaperTrail::RecordTrail#record_update. This is private API and is subject to change at any time without warning.
Jared Beck
  • 16,796
  • 9
  • 72
  • 97