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
human_attribute_name inconsistently provides attributes #36916
Comments
These changes were introduced by the i18n efforts at Shopify. I'm wondering if you could triage this / weigh in? There is an included repro script I hope helps! 🙏 thank you. |
Why it is important to be consistent? Do you have any code that rely on the argument of |
Up until Rails 6, only symbols were provided by the API. While looking into how to change the attribute name, the examples I came across throughout the web assumed that symbols were given. I do understand that docs may have provided string, but not everyone may have looked there. Also, the intention might have been to provide string/symbol, but that was not what the reality was. Implicitly the design was to expect symbols. I think it might be worth a line in Rails 6 release docs to let people know that I was lucky that I wrote a test that checked the error messages because of some other custom logic, otherwise I would not have caught the regression in our Rails 5->6 upgrade. I want to try and help others avoid that regression too. At minimum, this issue serves as an indexed google-able reference point if someone else experiences this issue 😆 |
That is not true. Even before 8d2f317, if you called
Not sure about this. This was always the case, not only in Rails 6.
Agree. At least this confusion will not happen anymore in the future if we have the documentation to direct people to it. |
This required separate input from the user though. In my case, I was surprised because the behaviour for built in validators changed. In Rails 5, The attached commit/PR changes that to make them strings, but the same attribute is sometimes a symbol still. While it may not have been documented to rely on it being a string or symbol, it was never explicitly stated so it was kind of assumed by some people (like me) to be a symbol given the behaviour of the validators in Rails 5 and under. Even if you could pass a string in, and some areas do, some places might not have realized this was intended behaviour. Adding copy like |
I'm fine with that. Can you open a PR? |
Sure! I'll check it out soon. |
#36922 :) |
This was always the intended API as per discussion in #36916, however versions before Rails 6 always passed a symbol. This means that some apps relied on symbols to be passed, this change is intended to note the change in behaviour so that apps can respond in kind.
Steps to reproduce
https://gist.github.com/jules2689/e6cc70793b3ee9c758ab1c1f293d9d0f
Run this script with and without
RAILS_NEXT=1
Expected behavior
Be given a consistent data type (symbol in Rails 5).
Actual behavior
In Rails 5, we are consistently given
symbol
objects. In Rails 6, we are inconsistently given strings and symbols.System configuration
Rails version: Rails 5.2.3 and Rails 6.0.0.rc2
Ruby version: Ruby 2.3.7
Other Info
Commit that broke this behavior:
8d2f317
PR that introduced this API change:
#33615
Concern
Existing applications may not run a
.to_s
or a.to_sym
, and in fact #33615 uses a.to_s
while code I'm reviewing uses a.to_sym
. Alternatively, some StackOverflow posts like this one have accepted answers that do not type cast at all.Therefore I'd think that this will introduce breaking API changes for a number of applications in production.
Solution
Here are two options I can see:
.to_s
or.to_sym
If we choose option 2, we could potentially include a message in the upgrade script in the case that it finds an instance of
human_attribute_name
in the code baseThe text was updated successfully, but these errors were encountered: