Rails 模型中不区分大小写的搜索

编程入门 行业动态 更新时间:2024-10-25 19:21:08
本文介绍了Rails 模型中不区分大小写的搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的产品型号包含一些项目

My product model contains some items

Product.first => #<Product id: 10, name: "Blue jeans" >

我现在正在从另一个数据集中导入一些产品参数,但名称的拼写存在不一致.例如,在另一个数据集中,Blue Jeans 可以拼写为 Blue Jeans.

I'm now importing some product parameters from another dataset, but there are inconsistencies in the spelling of the names. For instance, in the other dataset, Blue jeans could be spelled Blue Jeans.

我想Product.find_or_create_by_name("Blue Jeans"),但这将创建一个新产品,几乎与第一个相同.如果我想查找和比较小写名称,我有哪些选择.

I wanted to Product.find_or_create_by_name("Blue Jeans"), but this will create a new product, almost identical to the first. What are my options if I want to find and compare the lowercased name.

性能问题在这里并不重要:只有 100-200 个产品,我想将其作为导入数据的迁移来运行.

Performance issues is not really important here: There are only 100-200 products, and I want to run this as a migration that imports the data.

有什么想法吗?

推荐答案

你可能需要在这里写得更详细

You'll probably have to be more verbose here

name = "Blue Jeans" model = Product.where('lower(name) = ?', name.downcase).first model ||= Product.create(:name => name)

更多推荐

Rails 模型中不区分大小写的搜索

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

发布评论

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

>www.elefans.com

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