0

I have this piece of code for a search query in my ruby on rails application. It works locally on the rails local server but not when I deploy on Heroku. Here's the line of code for that query:

@courses = Course.where("name LIKE? ","%#{params[:search][:course].downcase}%").all

A little bit of context: Course is my database model and it has a field called "name", here I am searching for all the courses with names containing certain words as specified from the user, that query word is passed in via the form params.

UPDATE: Thank you Danilo and Brad! Solved with putting a space in between LIKE and ? and used ILIKE instead of LIKE.

@courses = Course.where("name ILIKE ? ","%#{params[:search][:course].downcase}%")
Venus
  • 3
  • 2

1 Answers1

1

You have to put a space after LIKE and the ?, and you don't need to call .all method when you are using where, it's redundant.

@courses = Course.where("name LIKE ? ","%#{params[:search][:course].downcase}%")
Danilo Cândido
  • 370
  • 4
  • 17
  • 1
    You may also want ILIKE. Here is my favorite way to achieve this https://stackoverflow.com/a/44664100/525478 – Brad Werth Oct 23 '18 at 03:25
  • 1
    Thank you very much, Danilo and Brad! It worked now on heroku! I put the spaces and also used ILIKE. – Venus Oct 23 '18 at 04:50