Rails 中不区分大小写的搜索

编程入门 行业动态 更新时间:2024-10-25 17:26:15
本文介绍了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 中不区分大小写的搜索

本文发布于:2023-10-20 09:02:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1510527.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大小写   中不   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!