为什么这个查询不能在Heroku上运行? [英] Why doesn't this query work on Heroku?

查看:118
本文介绍了为什么这个查询不能在Heroku上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图用RoR和Heroku实现简单的搜索。这在localhost上工作正常,但只要我推到Heroku,搜索将不会返回结果。我在本地使用SQlite,Heroku使用Postgres,对吗?我认为这是问题,但经过相当多的研究后,我似乎无法找到解决方案。任何想法可能会发生什么?

  def self.search(搜索)
如果搜索
where(title LIKE?OR description LIKE?,%#{search}%,%#{search}%)
else
find(:all)
end
end


解决方案

可能因为 SQLite的LIKE (部分)不区分大小写,但 PostgreSQL的LIKE 区分大小写。试试这个:

  where(LOWER(title)LIKE?or LOWER(description)LIKE?,%#{search PostgreSQL也具有ILIKE,它可以帮助我们更好地管理PostgreSQL数据库,作为LIKE的一个不区分大小写的版本,但SQLite(或标准SQL)不会使用ILIKE,除非您专门针对PostgreSQL。



并切换您的开发环境PostgreSQL,如果你发布到Heroku,你可以节省一点点的悲伤和困惑。

另一种可能性是你没有任何数据(或任何匹配的数据)在您的Heroku数据库。


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

解决方案

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.

这篇关于为什么这个查询不能在Heroku上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆