'Rails ActiveStorage versioning (with paper_trail or somthing like that)
I have a User model, which has one image.
class User < ApplicationRecord
has_paper_trail
has_one_attached :image
end
I decided to paper_trail gem because I need versioning for User attributes. However, I noticed that I don't know how to get versioning information for image.
PaperTrail::Version.where(item_type: "User").first.object
=> "---\nemail:XXXX\n'\ncreated_at: 2021-10-28 13:06:15.206202000\nupdated_at: 2021-10-28 13:06:19.789124000\n"
Does anyone else know how to get previous version of ActiveStorage? I'm using paper_trail gem, so it's the best if paper_trail can do it.
Solution 1:[1]
Ok, I'm a little late to the party, but I have found a need for version tracking on my ActiveStorage models. Here's what worked for me:
- Create a new file in
/config/initializerscalledactive_storage.rb - In the new
/config/initializers/active_storage.rbfile, add the following code:
Rails.application.config.to_prepare do
ActiveStorage::Record.has_paper_trail
end
- Reboot your Rails server
You should now see version tracking for all subclasses of ActiveStorage::Record.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tim Lowrimore |
