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

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

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

我想要产品。find_or_create_by_name("Blue Jeans"),但这将创建一个与第一个几乎相同的新产品。我的选择是什么,如果我想找到和比较小写的名字。

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

什么好主意吗?


当前回答

假设您使用mysql,您可以使用不区分大小写的字段:http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html

其他回答

有些人使用LIKE或ILIKE显示,但这些允许正则表达式搜索。在Ruby中你也不需要小写。您可以让数据库为您做这件事。我想可能会快一些。first_or_create也可以用在where后面。

# app/models/product.rb
class Product < ActiveRecord::Base

  # case insensitive name
  def self.ci_name(text)
    where("lower(name) = lower(?)", text)
  end
end

# first_or_create can be used after a where clause
Product.ci_name("Blue Jeans").first_or_create
# Product Load (1.2ms)  SELECT  "products".* FROM "products"  WHERE (lower(name) = lower('Blue Jeans'))  ORDER BY "products"."id" ASC LIMIT 1
# => #<Product id: 1, name: "Blue jeans", created_at: "2016-03-27 01:41:45", updated_at: "2016-03-27 01:41:45"> 

另一种选择是

c = Product.find_by("LOWER(name)= ?", name.downcase)

大写字母和小写字母只差一位。最有效的搜索方法是忽略这个位,而不是转换上下位等等。如果使用Oracle,请参见NLS_SORT=BINARY_CI等。

在postgres:

 user = User.find(:first, :conditions => ['username ~* ?', "regedarek"])

你也可以像下面这样使用作用域,把它们放在一个关注点中,并包括在你可能需要的模型中:

作用域:ci_find, lambda{|列,值| where("lower(#{column}) = ?", value.downcase)。第一}

然后像这样使用: 模型。ci_find(“列”、“价值”)