activegraph icon indicating copy to clipboard operation
activegraph copied to clipboard

Dont clear all associations

Open joshjordan opened this issue 3 years ago • 4 comments

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?

joshjordan avatar Dec 23 '20 04:12 joshjordan

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.

codeclimate[bot] avatar Dec 23 '20 04:12 codeclimate[bot]

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.

klobuczek avatar Dec 25 '20 10:12 klobuczek

@amitsuryavanshi could you look at that one?

klobuczek avatar Feb 07 '21 03:02 klobuczek

@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.

amitsuryavanshi avatar Feb 08 '21 06:02 amitsuryavanshi