Rails 中不区分大小写的搜索 [英] Case insensitive search in Rails

查看:45
本文介绍了Rails 中不区分大小写的搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到的搜索我特别不希望字符大小写重要的东西的最佳方法是:

The best way I've found to search for things where I specifically don't want character-case to matter is:

@tag = Rails.env.development? ? Category.where("LOWER(name) LIKE ?", "%#{params[:find]}%")[0] : Category.where("LOWER(name) ILIKE ?", "%#{params[:find]}%")[0]

我必须有 .env 查找器,因为我使用 Heroku,而且我不关心在我的开发机器上安装 PostgreSQL.不过,是不是有类似的东西:

I have to have the .env finder because I use Heroku, and I haven't cared to get PostgreSQL setup on my dev machines. Still, isn't there something like:

@tag = Category.find_by_name(params[:find], case_sensitive: false)

是否有可以传递给 Rails 中的 find_by 助手的选项?这将是一个不错的选择.

Are there options that we can pass to the find_by helper in Rails? This would be a nice one.

推荐答案

是的,Rails 通过 内置的 Arel 库或像 Squeel 这样的 gem.

Yes, Rails supports case-insensitive queries via the built-in Arel library or a gem such as Squeel.

这篇关于Rails 中不区分大小写的搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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