mysql - Strip special characters and space of a DB column to compare in rails -
i have 4 types of last_name:
- "camp bell"
- "campbell"
- "campbelljr."
- "camp bell jr."
now, in rails when user searched it's last name camp bell, want show 4 records. so, tried:
rails
stripped_name = params[last_name].gsub(/\w/, '') #=> "campbell" user.where("lower(replace(last_name, '/\w/', '')) ?", "#{stripped_name}%") give me 2 records following last_name:
- "campbell"
- "campbelljr."
i guess, because, mysql replace not working correctly regex.
any ideas?
edit
guys, sorry confusion. idea strip off special characters including space. i'm trying use \w regex.
for example, input can be: camp~bell... but, should still fetch result.
you can check both stripped_name without space , ones include both names seperated space this.
stripped_name = params[last_name].gsub(/\w/, '') split_names = params[last_name].split(" ") user.where('name ? or (name ? , name ?)', "%#{stripped_name}%", "%#{split_names[0]}%", "%#{split_names[1]}%") next step search complete array of split names not first two.
Comments
Post a Comment