neo4jrb / activegraph

An active model wrapper for the Neo4j Graph Database for Ruby.
http://neo4jrb.io
MIT License
1.4k stars 277 forks source link

Dont clear all associations #1641

Closed joshjordan closed 9 months ago

joshjordan commented 3 years ago

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?

codeclimate[bot] commented 3 years ago

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.

klobuczek commented 3 years ago

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 commented 3 years ago

@amitsuryavanshi could you look at that one?

amitsuryavanshi commented 3 years ago

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