Postgres 使用 Rails 进行不区分大小写的搜索
我的开发数据库是 SQLite,但我将应用程序部署到 Heroku,他们使用 PostgreSQL。
现在,有时如果我执行搜索,我会得到两个不同的结果,因为 PostgreSQL 区分大小写,但 SQLite 不区分大小写。
Rails 不应该标准化这些东西吗?我应该用什么方法来解决这个问题?
My development database is SQLite but I deploy my app to Heroku and they are using PostgreSQL.
Now sometimes I have two different results coming out if I perform searches because PostgreSQL is case-sensitive but SQLite is not.
Shouldn't Rails standardize these things? What methods should I use to solve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Postgres 中不区分大小写的搜索:
ilike
而不是like
(不区分大小写的 like) ,使两边都为 UPPER 或 LOWERCase insensitive searching in Postgres:
ilike
instead oflike
(case-insensitive like)处理此问题的另一种
DDL
方法是citext
数据类型,或不区分大小写的 TEXT。Another
DDL
way of handling this is thecitext
data type, or case-insensitive TEXT.ActiveRecord 无法处理许多相对常见的事情,LIKE 匹配就是其中之一。以下是如何使用直接 Arel 来实现此目的。
您可以安装 Arel-Helpers 以使这变得更容易,
我之前推荐Squeel 为此,但原始创建者已停止支持它,并且似乎没有全职开发人员维护它。由于它使用私有 ActiveRecord API,因此需要不断维护。
There are many relatively common things that ActiveRecord doesn't handle, and LIKE matching is one of them. Here is how to achieve this using straight Arel instead.
You can install Arel-Helpers to make this a little easier
I previously recommended Squeel for this, but the original creator has stopped supporting it and there doesn't seem to be a full-time developer maintaining it. And since it plays around with private ActiveRecord APIs, it needs constant tending.