1

I have a sql query which I have written in the following way:

SELECT "books".* FROM "books" WHERE (title LIKE '%intro%' OR isbn LIKE '%intro%' or status like '%intro%' or author like '%intro%' or description like '%intro%')

In my rails model I wrote the following way:

def self.search(query)
  where(['title LIKE ? OR isbn LIKE ? or status like ? or author like ? or description like ?', "%#{query}%", "%#{query}%","%#{query}%","%#{query}%","%#{query}%"])
end

This works on my sqlite3 database and returns all rows with the query "intro".

However in my postgres database it returns only one row. Could some one suggest me a way.

Contents of my table:

id  isbn    title   description author  status
2   15  Introduction to english Introduction to english veena kapoor    available
3   16  Introduction to hindi   Introduction to hindi   rastogi vinayak available
4   17  Introduction to sanskrit    Introduction to sanskrit    edward solomon  available
user1692342
  • 5,007
  • 11
  • 69
  • 128

1 Answers1

2

The condition WHERE title LIKE '%intro%' (for example) won't match the value 'Introduction to english' because LIKE is case-sensitive in Postgres. You must ILIKE to do a case-insensitive match.

This may be a problem, since ILIKE doesn't work in SQLite. Take a look at this question for some potential solutions: Generic Ruby solution for SQLite3 "LIKE" or PostgreSQL "ILIKE"? The best solution (as proffered by the top-rated answer there) is to use the same database (Postgres) in both development and production.

Community
  • 1
  • 1
Jordan Running
  • 102,619
  • 17
  • 182
  • 182