Rails - find_by and where is not working as expected

Rails version - 6.0.3.6

I have one table subscription_attempts and it has one column attempt_token which is unique random token for each record and it is generated based on rails’s has_secure_token.

I have one method which will try to find record based on attempt_token or id. Below is the method code

def random_method(value)
  find_by(id: value) || find_by(attempt_token: value)
end

It was working all fine until I got one attempt_token which is starting with a digit. Here is that attempt_token value - 5HqBToVVbFVL4T6TLzuuKju8

When I use that method it is giving me SubscriptionAttempt record with id 5. I have also tried where and the result is the same.

When i tried below code

SubscriptionAttempt.find_by('id = ?', '5HqBToVVbFVL4T6TLzuuKju8')

i am getting this exception

ActiveRecord::StatementInvalid (PG::InvalidTextRepresentation: ERROR:  invalid input syntax for type bigint: "5HqBToVVbFVL4T6TLzuuKju8")
LINE 1: ...empts".* FROM "subscription_attempts" WHERE (id = '5HqBToVVb...

I am assuming this is not how these methods should behave. It should give a nil result if the value is not matching with database value. Can someone please give me the solution to this problem?

I can offer a workaround.

/^\d+$/.match?(value) ? find_by(id: value) : find_by(attempt_token: value)