Ayer
Ayer

Reputation: 140

How to use custom version tables with meta in paper trail?

I wanted to track change of storages and store it in a storage_versions table which have a column storage_type to store the type of storage that is modified. I'm trying to do it like this:

class Storage < ApplicationRecord
    has_paper_trail(meta: {storage_type: :storage_type}), class_name: 'StorageVersion'
//additional methods here
end

And I've 'StorageVersion' class defined at app/models/paper_trail/storage_version.rb like this:

class StorageVersion < PaperTrail::Version
    self.table_name = 'storage_versions'
end

As far as I know it should work but it isn't. How can I make this work?

Update

Sorry for the incomplete question. I have versions table defined(not abstract), storage_versions table do have storage_type column.

Solution

Moved storage_version.rb outside of paper_trail folder and changed the declaration of has_paper_trail to

has_paper_trail class_name: 'StorageVersion', meta: { storage_type: :storage_type }

like @jemonsanto said. Thank you @jemonsanto!

Upvotes: 2

Views: 1913

Answers (1)

jemonsanto
jemonsanto

Reputation: 513

Could be myriads of reasons why it won't go through:

You haven't posted the schema, so do check there if storage type is declared.

If you don't have the versions table then it would surely break. You can fix that by declaring the base class as an abstract class per documentation.

Next,

Your class declaration needs to be fixed since it is on a folder, you need to add it to the module PaperTrail. Thus, your class should be, PaperTrail::StorageVersion.

If you don't wanna do that, you can just remove the class from subfolder and let it reside under models/

Your method declaration for has_paper_trail seems a bit wonky. You've excluded the class name in the params. Also noticed that you've put your class in app/models/paper_trail/storage_version, you need to specify that path in the method declaration.

has_paper_trail class_name: 'PaperTrail::StorageVersion', meta: { storage_type: storage_type }

Upvotes: 1

Related Questions