I have an email address and I would like to check if it is in different model, in which case, return the id in that model. I wrote the following:
def find_contact(email) case email when !User.find_by(email: email).nil? return User.find_by(email: email).id when !Viewer.find_by(email: email).nil? return Viewer.find_by(email: email).id else return "No information" end end When i try with any email, it returns No Information even for the current user. What am i doing wrong please ?
User.where(email: email).nil?will never be true, you'd wantUser.find_by(email: email).nil?orUser.where(email: email).exists?.emailmatches the condition of whether or not a field is present. Basically, you'll end up checking whethertrue === emailorfalse === email(Note that in Ruby,===is the case match operator), which is probably not what you want.User.find_by(email: email)&.id || Viewer.find_by(email: email)&.id || "No information"