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
Raise an exception when model without primary key calls .find_with_ids #12549
Conversation
Toy.primary_key = nil | ||
begin | ||
Toy.find(1) | ||
rescue ActiveRecord::UnknownPrimaryKey => e |
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.
what do yu think about using: assert_raises
instead of rescue
?
To complete PR needs to add Changelog entry. |
@pftg done. thanks! |
@@ -860,6 +860,15 @@ def test_find_one_message_with_custom_primary_key | |||
Toy.reset_primary_key | |||
end | |||
|
|||
def test_find_without_primary_key | |||
Toy.primary_key = nil |
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.
Can you use one of the existing models, that does in fact not have a primary key? Maybe Matey
?
EDIT: This way we don't need to mess with global state.
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.
Oh, I didn't know Matey
.
I use it. 17c08fd
@makimoto the PR does not apply, can you push a rebased version? |
ok, rebased and pushed. |
Raise an exception when model without primary key calls .find_with_ids
@makimoto thank you for your contribution 💛 |
I fixed exception handling for
.find
of models without primary key.Now
.find
by models without primary key raisesStatementInvalid
exception by the invalid SQL query, but I think an exception should be raised before building a query.By this PR
AR::UnknownPrimaryKey
are raised whenModel.primary_key
isnil
andModel.find
calls.