简体   繁体   English

Postgres Rails 4搜索查询ID或多列

[英]Postgres Rails 4 search query id or multiple columns

I am working in an app with a basic search form with Heroku, but I can't get my sql query to work properly with PostgreSQL, even though this query worked with MySQL. 我正在使用Heroku在具有基本搜索表单的应用程序中工作,但是我无法使sql查询与PostgreSQL一起正常使用,即使该查询与MySQL一起使用也是如此。 By the way, I tried to paste the logs from Heroku, but it only says that when you search something it renders 500.html. 顺便说一句,我试图粘贴Heroku中的日志,但是它只说当您搜索某些内容时,它将呈现500.html。

Here's my model OrdemDeServico with the search action: 这是带有搜索操作的我的模型OrdemDeServico:

def self.search(search)
   if search
    joins(:cliente).where("clientes.nome LIKE ? OR veiculo LIKE ? OR placa LIKE  ? OR ordem_de_servicos.id = ?", "%#{search}%", "%#{search}%", "%#{search}%", "#{search}")
   else
    where(nil)
   end
  end

I just installed PostgreSQL locally, and it returned this error when searching: 我刚刚在本地安装了PostgreSQL,并且在搜索时返回了此错误:

`PG::InvalidTextRepresentation: ERROR:  invalid input syntax for integer: "Augusto"
LINE 1: ... placa LIKE '%Augusto%' OR ordem_de_servicos.id = 'Augusto')

query: 查询:

SELECT  "ordem_de_servicos".* FROM "ordem_de_servicos" INNER JOIN "clientes" ON "clientes"."id" = "ordem_de_servicos"."cliente_id" WHERE (clientes.nome LIKE '%Augusto%' OR veiculo LIKE '%Augusto%' OR placa LIKE '%Augusto%' OR ordem_de_servicos.id = 'Augusto')  ORDER BY prazo LIMIT 5 OFFSET 0

I finally worked it out a solution. 我终于找到了解决方案。 Those who have the same problem here's my code for the model: 那些有相同问题的人是我的模型代码:

def self.search(search)
    if search
        where("id = ?", search)
        joins(:cliente).where("clientes.nome ilike :q or veiculo ilike :q or placa ilike :q", q: "%#{search}%")
    else
        where(nil)
    end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM