-
Notifications
You must be signed in to change notification settings - Fork 130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solution for tracking has_and_belongs_to_many relations #215
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_many
relations 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_many
relation. 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_many
relation 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: