-
Notifications
You must be signed in to change notification settings - Fork 106
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: Fix #find does not take string(s) as primary key(s). #643
base: main
Are you sure you want to change the base?
Conversation
ActiveRecord supports automatic conversion from String to Integer on `#find` call. The API document of `ActiveRecord::FinderMethods#find` says: > If the primary key is an integer, find by id coerces its arguments > by using to_i. > https://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find Unfortunately, it's difficult to add the `_ToI` type only if the primary key is an integer. So this adds it always.
@tk0miya Thanks for your contribution! Please follow the instructions below for each change. Available commandsYou can use the following commands by commenting on this PR.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it says “If the primary key is an integer”, I don't think it always allows _ToI
.
The actual #to_i
seems to be called with ActiveModel::Type::Integer
.
I would like to know your actual problem.
In my case, I passed a primary key typed as String read from CSV.
|
@tk0miya Is the following correct?
|
Ah, sorry. I missed your message.
Almost right. More precisely, our production code checks
In my thought, it's not strange that ID is converted to String when we interoperate with other systems. Some communication passes ensure serializing IDs to String. For example, CSV and HTTP query params represent all data as String. Rails and ActiveRecord support the use of String IDs easier by auto conversion. So I think adding As you said, the conversion works only if the primary key is an integer. So adding it to gem_rbs_collection might not be good. what do you think adding this to rbs_rails? If agreed, I'll post this to the repo later. |
If the primary key is a string, However, there may be some issue that makes this assumption incorrect, which is why I am asking what the problem is. With the current approach to the changes, I am unlikely to merge. |
ActiveRecord supports automatic conversion from String to Integer on
#find
call.The API document of
ActiveRecord::FinderMethods#find
says:Unfortunately, it's difficult to add the
_ToI
type only if the primary key is an integer. So this adds it always.Is it better to override the signature in rbs_rails side?