I’m implementing some simple change tracking logic using the ActiveRecord::Dirty methods.
Things are working great for changes directly on the model, however I am unable to figure out how to track the changes to has_many :through
associations when they are set via the collection_ids
method
For example there is not a way to track the changes to the IDs when performing this operation
post.comment_ids = [1,2,3]
This appears to be due to the fact that calling the collection_singular_ids=ids
method immediately issues a delete/insert/commit query on the join table, therefore there is no “pending” state on the model.
According to the docs, calling collection_singular_ids=ids
performs the same as collection=objects
collection=objects
Replaces the collections content by deleting and adding objects as appropriate. If the :through option is true callbacks in the join models are triggered except destroy callbacks, since deletion is direct by default. You can specify dependent: :destroy or dependent: :nullify to override this.
I have scoured the internet and this seems to be a known behavior and common issue in relevant change tracking gems.
My question: am I out of luck with this approach? Is there an alternative to track these changes?
Here’s some relevant sources: