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

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

我的产品模型中包含的某些项目

My product model contains some items

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

我现在进口的一些产品参数从另一个数据集中,但也有名字的拼写不一致。例如,在其他数据集,蓝色牛仔裤可以拼蓝色牛仔裤。

我想 Product.find_or_create_by_name(蓝色牛仔裤),但是这将创建一个新的产品,几乎等同于第一。什么是我的选择,如果我想找到并比较小写的名字。

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:04:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1510532.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大小写   模型   Rails

发布评论

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

>www.elefans.com

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