westonganger / paper_trail-association_tracking

Plugin for the PaperTrail gem to track and reify associations
MIT License
128 stars 38 forks source link

has_and_belongs_to_many #26

Open chumakoff opened 3 years ago

chumakoff commented 3 years ago

Whenever I update a main record, versions for hundreds associated through 'has_and_belongs_to_many' records are created. Is that an expected behavior?

class AnyModel < ApplicationRecord
  has_and_belongs_to_many :any_associations
end

record = AnyModel.last
record.any_associations.count # => 999999999999
record.touch  # this will result in 99999999999 "INSERT INTO `version_associations" SQL queries
westonganger commented 3 years ago

Yes this is probably what we are forced to do with HABTM relationships. I wouldnt recommend using HABTM relationships ever really, try using a has_many through relationship

Alternatively depending on the use case it may be an idea to add has_paper_trail only: [:revision_number] or something on the parent model to limit the number of version records created.