Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 254
Pirun Seng Avatar asked Oct 21 '25 14:10

Pirun Seng


2 Answers

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)
like image 147
Ghis Avatar answered Oct 23 '25 05:10

Ghis


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') }
like image 45
Andrey Deineko Avatar answered Oct 23 '25 04:10

Andrey Deineko