有没有一种更简洁、DRYer 的方法可以使用 Postgres 作为数据库在 Rails 5.0 中编写此代码?
CrmRevenueLineItem.references(:crm_account)
.where("crm_accounts.name ILIKE :qry OR crm_accounts.legal_name ILIKE :qry
OR crm_accounts.website ILIKE :qry", qry: wildcard_query)
我觉得我在里面重复了很多次。在我了解命名参数(:qry
)并使用 ?
作为占位符之前,情况更糟。
最佳答案
使用 concat_ws 的变体稍微短一点功能
CrmRevenueLineItem.references(:crm_account)
.where("CONCAT_WS(' ', crm_accounts.name, crm_accounts.legal_name, crm_accounts.website) ILIKE :qry", qry: wildcard_query)
关于postgresql - Rails where 子句中的多字段比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57965295/