Question

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?

Était-ce utile?

La solution

Regexp.escape escapes special characters:

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

Autres conseils

@companies = Company.where(name: /#{Regexp.escape(params[:search])}/i)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top