Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all versions of paper_trail when delting a model

I'm using the paper_trail gem for a blogging system to store versions of the article. However I don't need to keep any version of an article after it has been destroyed.

How can I delete all versions of a model instance when it's destroyed? I only see in the documentation a way to delete versions globally.

Any ideas?

like image 713
Nick ONeill Avatar asked Dec 27 '22 01:12

Nick ONeill


1 Answers

before_destroy do
  self.versions.destroy_all
end

By default has_paper_trail creates has_many :versions association. So you can easily destroy all of them. But make sure in your model you have this :

 has_paper_trail on: [:create, :update]  #because you dont want to keep record for destroy event.
like image 100
Muntasim Avatar answered Feb 11 '23 12:02

Muntasim