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

Fix bug with symbolized keys in .where with nested join (alternative to #27598) #27599

Conversation

NickLaMuro
Copy link
Contributor

Summary

In #25146, code was added to fix making where clauses against tables with an enum column with a join present as part of the query. As part of this fix, it called singularize on the table_name variable that was passed into the associated_table method.

table_name, in some circumstances, can also be a symbol if more than one level of joins exists in the Relation (i.e joins(:book => :subscription)). This fixes that by adding chaning the .stringify_keys! (found in ActiveRecord::Relation::WhereClauseFactory) to be a .deep_stringify_keys! to stringfy keys at all levels.

Other Information

This bug only surfaces when a join is made more than 1 level deep since the where_clause_builder calls stringify_keys! on the top level of the .where hash:

https://github.com/rails/rails/blob/21e5fd4/activerecord/lib/active_record/relation/where_clause_factory.rb#L16

So this hides this edge case from showing up in the test suite with the current coverage and the test that was in PR #25146.

This is the alternative to #27598 in which the change from PR #25146 was fixed in isolation. Instead, here we fix the false assumption that all table_name values being passed into .associated_table are a string. This might have wider effects because of that, so that should be considered when reviewing.

This will cause a failure with the changes from 8e2e5f9:

rails@8e2e5f9

With the `singularize` call that is being done in that method when there
is multiple nestings of associations (JOIN calling a JOIN) and the
`stringify_keys!` is only called once here:

https://github.com/rails/rails/blob/21e5fd4/activerecord/lib/active_record/relation/where_clause_factory.rb#L16

And not in the subsequent recursion in `.predicate_builder`
Since it is expected that top level keys are in their string form, not
symbol, make sure that keys on all levels are now strings so that
subsequent methods that expect only strings for keys always get them.
@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @sgrif (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@NickLaMuro
Copy link
Contributor Author

cc: @maclover7 (since you were involved with #25146 )

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.

None yet

4 participants