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 index_errors and provide :nested_attributes_order mode #48727
Conversation
Hi @amatsuda may I ask for your review please if you have time? Sorry I don't know the current process as Rails bot doesn't seem to auto assign anymore. The lint error seems like a false positive. |
@guilleiguaran @rafaelfranca apologies if this message is not okay with you but is there any chance you have some bandwidth to review this PR? This could be a good solution to this bug that has prompted lots of monkey patches. Thank you! |
This would really solve a problem that a lot of people are having to work around in #24390. |
I talked with Eileen at RubyConf and she mentioned sharing the PR in the discord server. I can do it later, but if anyone can do it before me, that'd be awesome ⭐ |
Hey team, I'm just cleaning up some code and came across a monkey patch we have for this. I thought I'd take a moment to toss my hat in the "would love this" pile. Hopefully y'all have time to review soon 🙏 |
In a desperate attempt to prevent this important PR from becoming stale: we need the fix in this PR to keep the errors our Rails API returns to the frontend in sync with the actual objects, too. Please consider reviewing and merging it. It will make Rails better. =) Thanks a lot! |
This is much needed feature for getting errors indexed by params we pass. |
@lulalala, could you please fix this PR to make it easier for the maintainers to merge it? Thanks a lot for everything you put into this already! 👍🏼 |
Would you mind fixing the conflicts please? I think this is fine. |
which respects reject_if and is in nested_attributes order. When in default index_errors:true mode, fix rails#24390 and return index based on full association order.
@lulalala thank you! |
@tenderlove Thank you! I was going to to check out the CI result in the morning before telling you. |
HOLY SHIT! THANK YOU! THANK YOU @lulalala AND @tenderlove!!! |
thank you @tenderlove for letting me simply drop by and mention this one during Hack Day at RailsConf 2024. It's a RailsConf hack day miracle 🎉 |
Thank you! 🙏 |
Thank you so much @stefannibrasil for bringing this up. |
index_errors: :nested_attributes_order
mode for an alternative ordering of errors.Motivation / Background
GitLab is using
index_errors
but has to workaround its bug, which is #24390. That bug is about association validation error having the incorrect index because the indexing can be computed from an incomplete collection of association records. The fix would be to index from the full association collection.@tijwelch first created #24728, however @markedmondson pointed out that
reject_if
would not work properly. Also, from the discussions I found that people have two different interpretations of what "indexing" means:reject_if
is only applicable here)Those two are conflicting goals. To cater to both of them, this PR also adds a new ordering mode called
nested_attributes_order
. This mode is more applicable to GitLab's use case, where the frontend could pass nested attributes in arbitrary order, and still want the error index to match such order.Detail
This Pull Request adds a new class called
ActiveRecord::Associations::NestedError
, which handles the calculation of index. The original index logic exists inAutosaveAssociation
and is moved into this class. Base onindex_errors
setting, it would choose whether to index based onassociation.target
order or nested attributes order.For nested attributes order, when nested_attributes are assigned (via a setter like
roles_attributes=
), the array of the corresponding records will be stored in the association object asnested_attributes_target
. TheNestedError
can then access this array to compute the index. Thereject_if
would also work since if something is rejected, thenested_attributes_target
would havenil
in its place as a placeholder, therefore maintaining the overall index.Additional information
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]