So I want to be able to get an object using find_by_id_or_name, I feel like I saw another question like this but am trouble finding any resources on making my own finder.
So I want to be able to get an object using find_by_id_or_name , I
Share
You can do this by adding a class method to your model e.g.
You will then by able to do
You can customise the method slightly depending on your requirements e.g. if you want to try by
idfirst and then bynameyou could useModel.exists?first to see if there is matching record before doing afind_by_name. You could also look ifid_or_nameconsisted of characters0-9and assume that was anidand only search by name if it contained other characters.