我的产品型号包含一些项目
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 模型中不区分大小写的搜索
发布评论