Pirun Seng
Pirun Seng

Reputation: 443

How To Skip PaperTrail Version on ActiveRecord Callback

Currently, I've implemented a Rails Data Auditing app using PaperTrail. I've added a new version for every create, update and destroy actions.

The thing is that, I have an after_create callback in my model in which I want the PaperTrail to ignore it.

For now, when I create a new record, there are two versions are created. One is of the create event, and other one is of the update event of my callback.

What I want is to just one version to be created here, a version of the create event.

To be specific, my callback is just to add a slug, an alias is, to my record in which I do not want to show such this change to the user.

I wonder if there is a way to do so.

Any suggestions are appreciated.

Thanks,

Upvotes: 4

Views: 3041

Answers (2)

Ghis
Ghis

Reputation: 913

without_versioning was removed in v10, as mentionned here.

Use instead :

PaperTrail.request(enabled: false) do
  # no versions created
end

Or for specific model only :

PaperTrail.request.disable_model(Banana)
# changes to Banana model do not create versions,
# but eg. changes to Kiwi model do.
PaperTrail.request.enable_model(Banana)

Upvotes: 7

Andrey Deineko
Andrey Deineko

Reputation: 52357

I think you can make use of without_versioning, which

Executes the given method or block without creating a new version

user.without_versioning { |obj| obj.update_attributes(attribute: 'new_value') }

Upvotes: 4

Related Questions