Conversation
Here's an example of a CHANGELOG.md entry: * [#215](https://github.com/mongoid/mongoid-history/pull/215): Solution for tracking has_and_belongs_to_many relations - [@mikwat](https://github.com/mikwat).Generated by 🚫 danger |
|
Carryover of conversation from #214:
|
|
I've tried to build something by following the embeds model. I can get to the association, but don't know how to insert the callback from the association. WIP in dblock@1209422. |
|
I like the direction you're going with this @dblock, but I think we're running into the same problem. |
|
So we can add the callback by overwriting |
|
We should only modify the callbacks in cases where the habtm attributes have been explicitly whitelisted. |
|
See #217 |
Currently,
has_and_belongs_to_manyrelations are not tracked consistently. For example:Given the following models:
Create with tags works as expected:
However, changes are not recorded:
Unfortunately, none of the document callbacks (i.e.
around_update,around_create,around_destroy) are called when modifications are made to ahas_and_belongs_to_manyrelation. From my reading of the Mongoid docs and code, the only way to be notified of changes to these relations is to attach a callback directly to the relation definition.While inconvenient, this proposed solution simply makes a callback method available that must be attached to each
has_and_belongs_to_manyrelation viabefore_add: :track_has_and_belongs_to_many, before_remove: :track_has_and_belongs_to_many. I don't know of a way to automatically attach these callbacks, but please educate me if this is possible.Here's an example of this solution in use: