rails
rails copied to clipboard
Fix _read_attribute so it uses aliased attribute name as well
Summary
The read_attribute method takes attribute_alias into consideration so _read_attribute should also do.
Fixes https://github.com/rails/rails/issues/45632.
This looks like it would be losing the speed gained in 7834363, definitely want a benchmark to see for sure.
Edit: maybe removing name = attr_name.to_s
would be sufficient?
@skipkayhil thank you, we did not need to convert attr_name to string. I have updated accordingly to your feedback, please have a look
_read_attribute
and _write_attribute
are a private API which had introduced at 08576b94ad4f19dfc368619d7751e211d23dcad8 (be2b98b4ae3397149b713b774e415143c88c4fb7) and #29495 to bypass the slower parts that checks for attribute aliases and custom primary keys.
You can fix the association's attribute checks to aware of attribute aliases without sacrificing performance of the private API.
See also #39495, #39501.
I think we should benchmark to see what the impact is. I'm not too worried about the hash lookup, a bit more about the self.class.attribute_aliases
part.
If the impact is negligible, doing alias resolution here would allow to simplify a lot of callers. Because that's far from the first time I see a PR adding alias_attributes support in some part of Active Record, which suggest we're not doing it at the right abstraction level.
If we don't intend to bypass attribute alias resolution, we can just use the read_attribute
public API.
Now, read_attribute
has also been improved as much as possible.
See 5575bd7b22e8e11ba8e2fdac4a92aab931d4f9f9, #36052, 27a1ca2bfeda4298bbf44da17d07fac4147a4b1c.
In other words "You can fix the association's attribute checks to aware of attribute aliases" in https://github.com/rails/rails/pull/45633#issuecomment-1198464204, if we revert be2b98b4ae3397149b713b774e415143c88c4fb7, the script in #45632 will pass I think.