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}%")
1
  • What is the error on Heroku? Please edit your question to include the error stack. Commented Oct 23, 2018 at 3:20

1 Answer 1

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}%")
Sign up to request clarification or add additional context in comments.

2 Comments

You may also want ILIKE. Here is my favorite way to achieve this stackoverflow.com/a/44664100/525478
Thank you very much, Danilo and Brad! It worked now on heroku! I put the spaces and also used ILIKE.

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.