-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
find_or_create_by! behavior raises RecordNotFound in Rails 7.1 #51149
Comments
cc @fatkodima @casperisfine -- since you guys will have the most context having fixed the other related issue. |
This was changed in #45720. I think, the correct use was like the following (even before that change): 2.times do |t|
begin
Tag.find_or_create_by!(foo: 'Foo', foo_id: 1) do |tag|
tag.name = "foo_#{t}"
end
rescue ActiveRecord::RecordNotUnique
# some app logic that handles RecordNotUnique
end
end |
Yeah, I agree with @fatkodima here, only the unique keys should be passed as keyword arguments, it's evidenced by the performed queries on both versions:
Note the:
I understand that you were relying on that behavior, but that never was the intended use. Maybe you could extend a bit more on what your use case is? The reduced test case is nice for showing the change in behavior, but doesn't help figuring out if there is a legitimate reason to expose such behavior or if you were better served by using it differently. |
This makes sense to me. We can close out this issue. We'll address the code on our end. Thanks! |
Steps to reproduce
Expected behavior
This is almost the same as #48035 but without concurrency involved.
In Rails 7.0,
find_or_create_by!
with duplicated uniqueness constraints raisesActiveRecord::RecordNotUnique
errorActual behavior
In Rails 7.1, the above example raises
ActiveRecord::RecordNotFound
with following sql queries:System configuration
Rails version: 7.1.3
Ruby version: 3.3.0
The text was updated successfully, but these errors were encountered: