user16012143
user16012143

Reputation: 131

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.

Upvotes: 4

Views: 1107

Answers (1)

Tim Lowrimore
Tim Lowrimore

Reputation: 2052

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.

Upvotes: 3

Related Questions