本文介绍了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 中不区分大小写的搜索
发布评论