-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
ActiveRecord::refresh() should not unset all relations but only the ones whose corresponding foreign key has changed #19785
Comments
Yes. Ideally it should behave like you've described. Do you have time for fixing it? |
This particular issue causes a bunch of problems in my production code, I will submit PR shortly. |
@PowerGamer1 Just to be sure, your example states |
Yes, you are correct, I edited my original post and fixed it. |
Are you sure about that? The |
Your quote from docs mentions only "... THIS active record ..." and the relations are DIFFERENT active records. But that is irrelevant - see below.
You don't know but the user does. So why are you screwing the user by force-clearing his relations instead of letting him handle the clearing himself if needed (see example 7 from #19788)? P.S. The problem in this issue is a part of much bigger problem in #19788 and must not be considered in separation. |
The #13618 introduced "magic" functionality that makes it look like changing foreign key attribute of a model also changes loaded relation model dependent on this foreign key. This was achieved by simply unsetting the affected relation (whose foreign key has changed).
This was actually a BC breaking change later documented in UPGRADE.md as:
Unfortunately, the original implementation didn't follow its own logic thoroughly and left the
ActiveRecord::refresh()
implemented incorrectly - unsetting ALL relations regardless of changes to the foreign keys. And so, if relations loaded before refresh() are used again after refresh() additional queries against database are executed to fetch the same data recently unset by refresh().What steps will reproduce the problem?
What is the expected result?
false
true
What do you get instead?
false
false
Additional info
The text was updated successfully, but these errors were encountered: