Why doesn't this query work on Heroku?

痞子三分冷 提交于 2020-01-06 08:13:33

问题


Trying to implement a simple search with RoR and Heroku. This works fine on localhost but as soon as I push to Heroku, the search returns no results. I'm using SQlite locally and Heroku uses Postgres, correct? I think this is the problem but after quite a bit of research I can't seem to find the solution. Any idea what might be going on? Help appreciated as always.

def self.search(search)
  if search
   where("title LIKE ? OR description LIKE ?", "%#{search}%", "%#{search}%")
  else
   find(:all)
  end
end

回答1:


Probably because SQLite's LIKE is (partially) case insensitive but PostgreSQL's LIKE is case sensitive. Try this:

where("LOWER(title) LIKE ? OR LOWER(description) LIKE ?", "%#{search.downcase}%", "%#{search.downcase}%")

PostgreSQL also has ILIKE as a case insensitive version of LIKE but SQLite (nor standard SQL) doesn't so you shouldn't use ILIKE unless you are specifically targeting PostgreSQL.

And switch your development environment to PostgreSQL if you're publishing to Heroku, you will save yourself a fair bit of grief and confusion.

Another possibility is that you don't have any data (or any matching data) in your Heroku database.



来源:https://stackoverflow.com/questions/7135375/why-doesnt-this-query-work-on-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!