Skip to content
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

Reset @association_scope when resetting an association #42643

Closed
wants to merge 4 commits into from

Conversation

ollieh-m
Copy link

@ollieh-m ollieh-m commented Jun 29, 2021

Summary

Fixes #42637

When we load an association (active_service_contract) on an object (car), then reset that association (car.association(:active_service_contract).reset), the next time the association is loaded should trigger a fresh query to find any associated record.

This PR aims to ensure that's the case.

After loading an association on an object then resetting the association, calling save on the object has the effect of loading the association again (as part of AutosaveAssocation#save_has_one_association). Currently, when the association is loaded in this way, the query to find the associated record is based on the association_scope as it was evaluated originally. The conditions are not re-evaluated, meaning they can be stale. (In the test case in this PR, an expired service_contract is returned as if its expiry is still in the future, because the timestamp in the scope has become out-of-date.)

With this PR, the stale association_scope is reset when we call Association#reset, so when the association is next loaded (including as part of AutosaveAssocation#save_has_one_association), the association_scope gets re-evaluated and the values included in it are current.

@ollieh-m ollieh-m force-pushed the reset-scope branch 3 times, most recently from 6a45e54 to 85f9469 Compare June 29, 2021 20:59
@ollieh-m ollieh-m changed the title Reset scope Reset @association_scope when resetting an association Jun 29, 2021
@ollieh-m ollieh-m marked this pull request as ready for review June 29, 2021 21:11
@intrip
Copy link
Contributor

intrip commented Jun 30, 2021

@ollieh-m Honestly I don't know enough to say if calling reset_scope on reset is the right approach, in any case I've left a few minor comments 😃 . FF to check them if you want.

@rails-bot
Copy link

rails-bot bot commented Sep 28, 2021

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Sep 28, 2021
@ghiculescu
Copy link
Member

I think we still want this, could you rebase?

@rails-bot rails-bot bot removed the stale label Sep 28, 2021
@ollieh-m
Copy link
Author

I think we still want this, could you rebase?

Done 👍

@rails-bot
Copy link

rails-bot bot commented Dec 27, 2021

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Dec 27, 2021
@rails-bot rails-bot bot closed this Jan 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Association reset does not work as expected when you then call save
3 participants