Вопрос

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?

Это было полезно?

Решение

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.
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top