0

I am having trouble searching a string by the first letters. For example, if I have "animal", "ant", and "abs" in my database, I would like a query of "an" to return "animal" and "ant" (case insensitive). I got this working just fine with sqlite3:

Thing.where("name LIKE ?", params[:query] + '%')

However, this does not work in PostgreSQL (database used in pushing to Heroku). What is the correct query using PostgreSQL?

2 Answers 2

2

In PostgreSQL, you have to use ILIKE instead of LIKE to do a case-insensitive pattern match.

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

Comments

1

Your query will be:

Thing.where("name LIKE '?%'", params[:query])

This makes the query case insensitive:

Thing.where("lower(name) LIKE '?%'", params[:query].to_s.downcase)

If your production uses Postgres, I recommend development does it also, to prevent a further error like this!

1 Comment

Ah just figured it out. I used where("lower(name) LIKE lower(?)", params[:query] + '%') instead, but I Iike yours.

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.