Reify only one has_many :through association with PaperTrail?
Asked Answered
N

0

6

I'm using the paper_trail-association_tracking gem to save PaperTrail versions for AR associations. I have a model, let's call it User, that has many Groups and has many Skills. Both of these associations have join tables (user_groups and user_skills). So my User model has two has_many :through associations. All of the models (User, Group, Skill, UserSkill, UserGroup) have PaperTrail enabled on them.

class User < ApplicationRecord
  has_paper_trail

  has_many :groups, through: user_groups
  has_many :user_groups

  has_many :skills, through: user_skills
  has_many :user_skills
end

Say I want to reify an earlier version of the User model, along with their Skills (user_skills records), but not revert to an earlier version of the user's Groups (user_groups). The code looks something like this:

me = User.find(1)
older_me = me.versions.last.reify(has_many: true)
older_me.save

This will reify both UserGroups and UserSkills for this User, but I want it to reify just UserSkills. It seems the gem will reify all associations automatically, based on this source code I looked through.

So is there a way to specify which association I want to be reified? Or is it not best practices to do this? For my use case, I can't see a problem with this unless the user itself was actually deleted or something like that.

Sorry my models here sound kinda weird, I tried to use something generic and simplified, my real use case and models are a bit complex but this question demonstrates the bottom line of my issue.

Nucleon answered 22/10, 2019 at 18:49 Comment(3)
I think your review of the PT-AT source is correct. There seems to be no PT-AT feature that does what you want, as of today, 2019-10-24.Inclusive
Maybe you can use a temporary variable x to remember the user's groups and when the reify is done, set user.user_groups = x?Inclusive
@JaredBeck ah ok. Yeah that's essentially what I ended up doing to get around it but I'll leave this question open. Sounds like it might end up being more of a feature request.Nucleon

© 2022 - 2024 — McMap. All rights reserved.