Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 891
user16012143 Avatar asked Oct 18 '25 19:10

user16012143


1 Answers

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:

  1. Create a new file in /config/initializers called active_storage.rb
  2. In the new /config/initializers/active_storage.rb file, add the following code:
Rails.application.config.to_prepare do
  ActiveStorage::Record.has_paper_trail
end
  1. Reboot your Rails server

You should now see version tracking for all subclasses of ActiveStorage::Record.

like image 128
Tim Lowrimore Avatar answered Oct 20 '25 09:10

Tim Lowrimore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!