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
Conversation
6a45e54
to
85f9469
Compare
@association_scope
when resetting an association
@ollieh-m Honestly I don't know enough to say if calling |
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. |
I think we still want this, could you rebase? |
f2df9ad
to
845270e
Compare
Done 👍 |
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. |
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 ofAutosaveAssocation#save_has_one_association
). Currently, when the association is loaded in this way, the query to find the associated record is based on theassociation_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 expiredservice_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
isreset
when we callAssociation#reset
, so when the association is next loaded (including as part ofAutosaveAssocation#save_has_one_association
), theassociation_scope
gets re-evaluated and the values included in it are current.