0

I have a Ruby on Rails app, and I am trying to perform a search through a model. From the front end, I am passing the query string.

In courses_controller.rb I have the following code:

 ...
 Course.where("courses.name ILIKE ?", "%#{params[:search]}%")
 ...

If params[:search] is equal to something like "marketing", it will retrieve the item with name "marketing course". But if params[:search] is equal to "marketing course", nothing is retrieved.

How can I perform search with multiple words?

Thanks a lot.

1

2 Answers 2

1

Probably you need to check the value of params[:search].

Spaces in query string can be encoded with + or %20 chars.

So, in this case, you're trying to find something like marketing+course or marketing%20course which doesn't exist in the database.

Sign up to request clarification or add additional context in comments.

Comments

0

Turns out I found a way:

 ...
 Course.where("courses.name ILIKE ?", "%#{params[:search].tr('-', ' ')}%")
 ...

It was adding unwanted "-" between words.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.