I have 4 types of last_name:
- "Camp Bell"
- "CAMPBELL"
- "CampBellJr."
- "camp bell jr."
Now, in rails when an user is searched by it's last name like camp bell
, I want to show all the 4 records. So, I tried:
RAILS
stripped_name = params[last_name].gsub(/\W/, '')
#=> "campbell"
User.where("LOWER(REPLACE(last_name, '/\W/', '')) LIKE ?", "#{stripped_name}%")
Give me only 2 records with following last_name:
- "CAMPBELL"
- "CampBellJr."
I guess, this is because, the mysql REPLACE is not working correctly with regex.
Any ideas?
EDIT
Guys, sorry for the confusion. My idea is to strip off all special characters including space. So I'm trying to use \W
regex.
For example, the input can be: camp~bell..
. But, it should still fetch result.