westonganger/paper_trail-association_tracking

Associations not tracked without main model changed

bragovo opened this issue · 4 comments

Associations tracked only when main model was changed.

class User < ApplicationRecord
  has_many :locations
  has_paper_trail
end

class Location < ActiveRecord::Base
  belongs_to :user
  has_paper_trail
end
# Doesnt save changes
User.find(1).update(locations_ids: Location.pluck(:id).sample(2)) 

# Saves changes
User.find(1).update(name: 'qqq', locations_ids: Location.pluck(:id).sample(2))

Yes this behavior actually makes sense to me. In the first example you would need to update the updated_at attribute or another one to create a version on the User model.

This is made especially or there are some problems to get this changes?
Is it possible to add some option to track this, like PaperTrail.config.track_associations_always = true?

I think there is a workaround by changing updated_at in before_update for whole ApplicationRecord, but I dont know is that good idea or not.

No you are misunderstanding how paper_trail and PT-AT work. Updating a model/versioning is to be explicitly defined by each developer in their own application. I recommend you do some further research on how this all works.

Hi, does it work if you add touch: true to your belongs to association? Something like

class User < ApplicationRecord
  has_many :locations
  has_paper_trail
end

class Location < ActiveRecord::Base
  belongs_to :user, touch: true
  has_paper_trail
end

I think this should cause the updated_as timestamp of the User model to be updated whenever the Location model is changed which should then trigger a new User version.