Rails / Papertrail: changes with association changes

I am stuck. I was trying to figure out how to incorporate association changes ( has_many , has_many through ) into a model that has papertrail. I would like to call MyModel.versions.first.changeset , and any changes that have occurred to related objects will be included in the .changeset hash that is returned from this version of the object.

I added migrations for version associations:

 class CreateVersionAssociations < ActiveRecord::Migration def self.up create_table :version_associations do |t| t.integer :version_id t.string :foreign_key_name, :null => false t.integer :foreign_key_id end add_index :version_associations, [:version_id] add_index :version_associations, [:foreign_key_name, :foreign_key_id], :name => 'index_version_associations_on_foreign_key' end def self.down remove_index :version_associations, [:version_id] remove_index :version_associations, :name => 'index_version_associations_on_foreign_key' drop_table :version_associations end end class AddTransactionIdColumnToVersions < ActiveRecord::Migration def self.up add_column :versions, :transaction_id, :integer add_index :versions, [:transaction_id] end def self.down remove_index :versions, [:transaction_id] remove_column :versions, :transaction_id end end 

I added Papertrail to related objects, but, as far as I can tell, there is no documentation that discusses how to retrieve changes that occurred on related objects. Can anyone help if possible using Papertrail?

I am trying to implement a control trace of changes in a model and related objects, which can be accessed in one set of changes.

+5
source share
2 answers

The information you need is ultimately stored in the corresponding versions and version_associations tables.

However, paper_trail does not provide methods for accessing information the way you want. But you can write your own method yourself to get a list of version associations of an object.

Let's say you have the following models:

 class Article < ApplicationRecord has_many :comments has_paper_trail end class Comment < ApplicationRecord belongs_to :article has_paper_trail end 

You can find all versions of the comment object of the article article as follows:

 PaperTrail::Version.where(item_type: 'Comment') .joins(:version_associations) .where(version_associations: { foreign_key_name: 'article_id', foreign_key_id: article.id }) .order('versions.created_at desc') 

You can defuse the stone or define this method as an instance method of the Article class so that you can easily call it, for example article.comment_versions

Please note: the above information is not available in article.versions.first.changeset , because if you change the comment, but not the article, the article is not a version, but only a comment.

But the above method allows you to access the history of changes in associations.

0
source

It looks like it was added as an experimental feature for the papertrail gem

Check out the docs here. https://github.com/airblade/paper_trail/blob/v4.2.0/README.md#associations

This change will require the addition of a new database table for papertrail to track related models.

-1
source

All Articles