【问题标题】:Rails search string works under Sqlite3 but not under postgres in productionRails 搜索字符串在 Sqlite3 下有效,但在生产中的 postgres 下无效
【发布时间】:2016-01-14 21:34:51
【问题描述】:

我有一个搜索字符串,旨在在 3 个字段中查找关键字(搜索词),然后与地理区域进行比较。它在 Sqlite3 中有效,但在 Postgres(生产)中唯一有效的部分是地理搜索。我知道这一定是搜索字符串不符合 Postgres 的问题,但我找不到问题。

def self.search(params)
  listings = Listing.all
  listings = listings.where("title LIKE ? OR description LIKE ? OR shortdescription LIKE ?", "%#{params[:search]}%", "%#{params[:search]}%", "%#{params[:search]}%") if params[:search].present?
  listings = listings.near(params[:location], 75) if params[:location].present?
  listings
end

【问题讨论】:

  • “不起作用”具体是什么意思? params[:search] 中的内容是什么,当它不起作用时,您希望它找到什么记录?顺便说一句,一旦你解决了这个问题,你就会在你的开发环境中安装 PostgreSQL,对吧?
  • 搜索没有返回任何结果。搜索字符串应在 :title、:description 和 :shortdescription 中查找单词或短语。但它不返回任何结果。但它不会引发错误。我想我会听从你的建议,然后把 PostgreSQL 放在我的开发中。
  • 我刚刚发现它正在工作,但在搜索中区分大小写。我能应付!
  • LIKE 是否区分大小写取决于数据库,在 PostgreSQL 中,您将使用 ILIKE 来获得不区分大小写的 LIKE 版本。 ActiveRecord 并没有为您提供有用的数据库可移植性。所以,是的,忙着让 PostgreSQL 在你的开发环境中运行 :)

标签: ruby-on-rails postgresql production


【解决方案1】:

此代码确实有效 - 它只是区分大小写。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多