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_by(nil) returns first record instead of nil #14867
Comments
Hey @robertoplancarte, I'm not sure that's inherently true. find_by is a straight alias to where(args*).take, so really the question your asking is what should
do. I think if you treat where as an operation on a set of results, the noop for that operation is to return the same operation. If nothing else, I think changing this would pretty substantially change the API. Is there a use case that this makes a lot more sense in? |
It was just odd, I was expecting it to generate a query with "WHERE NULL" at the end. I dind't know find_by and find_by! were just an alias to where(args_).take and where(args_)take.! Thanks. |
I guess we can close, right? @estsauver thanks for explaining :) |
This topic confused me as well. I would prefer that I can see how a "noop" for a It would change the API, but what scenario would Of course, this is just my opinion. I'm curious what other think if anyone else visits and wants to join the conversation. |
I just ran into this and agree that it is very confusing that find_by(nil) returns a record. This does not seem like intuitive behavior here. |
Is it too late to change : D |
On the other hand, if you view So, changing your perspective makes this not so confusing and more intuitive. |
I agree. It makes no sense to find a certain record, whatever it is, based on no condition at all. I already pointed a semantically inconsistent construction in this issue and now it seems there is another. |
The argument that I am also not sure the |
I'm not arguing that a the API should change someone's perspective, but I'm saying that I could see it interpreted both ways. I think it's clear that the I think there are only two options to interpret how the
As far as I know, SQL will always error if the query's Would it make more sense to return a SQL syntax error when the argument is |
On any Model find_by(nil) will return the first record or nil if there are no records. find_by(nil) should always return nil. On the other hand find_by!(nil) does raise ActiveRecord::RecordNotFound as it should.
The text was updated successfully, but these errors were encountered: