Pregunta

In my rails application I am searching for company name and I am using mongodb for backend. My query for finding company is

@companies = Company.where(name: /#{params[:search]}/i).all

In my db I have company having name "ABC&D Services Inc. (Atlanta Project)" and When I am trying to search this company with giving string "ABC&D Services Inc." it will not return any result. Also if I am trying to search with string "ABC&D Services Inc.(" it gives me error end pattern with unmatched parenthesis

Anyone have idea how to solve this kind of issues with reg-ex. End user can perform search with any input so there is way to handle this?

¿Fue útil?

Solución

Regexp.escape escapes special characters:

@companies = Company.where(name: /#{Regexp.escape(#{params[:search]})}/i).all

Otros consejos

@companies = Company.where(name: /#{Regexp.escape(params[:search])}/i)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top