activegraph
activegraph copied to clipboard
Dont clear all associations
Ran across an odd case in our application that led me to discover this TODO
. We have some code that sets two associations, e.g:
@contact.emails = [Email.find(email_id)]
@contact.owner = current_user
@contact.save!
Strangely, save!
would throw an exception because the contact validation would fail for having no email. And, indeed, after the @contact.owner=
call, @contact.emails
would be empty. I found a nasty TODO that turned out to be masking other silent failures in our application as well, where unrelated cached associations would be cleared every time an association setter was called.
Again, I suspect this is not the right fix and I'd like your guidance on what I should do here to make a proper fix. Also, what is the appropriate spec file in which to test this?
Code Climate has analyzed commit dea6f8e9 and detected 1 issue on this pull request.
Here's the issue category breakdown:
Category | Count |
---|---|
Style | 1 |
View more on Code Climate.
Thanks for this. It looks like indeed a problem. I wonder why the original authors decided to keep it like that with a comment and not fixing it. Maybe we both are missing something. This hasn't been probably detected for that long time because there is a better api usage for your case:
@contact.update!(email_ids: [email_id], owner: current_user)
The assignment operators save immediately without calling callbacks.
@amitsuryavanshi could you look at that one?
@klobuczek @joshjordan This is a known issue, where entire association cache is cleared when we do a assignment on association. We need to check all possible scenarios with the fix provided.