Sql 如何修复此搜索方法
我的模型中包含以下内容,用于搜索用户输入的字符串:Sql 如何修复此搜索方法,sql,ruby-on-rails-3,Sql,Ruby On Rails 3,我的模型中包含以下内容,用于搜索用户输入的字符串: def self.find_products(product="") find(:all, :select => 'product_id, name', :order => "name", :conditions => ["name like ? and locale =?", "%#{product.capitalize}%", I18n.locale]) end 但它只在字符串是一个单词时起作用。 如果字符
def self.find_products(product="")
find(:all, :select => 'product_id, name', :order => "name", :conditions => ["name like ? and locale =?", "%#{product.capitalize}%", I18n.locale])
end
但它只在字符串是一个单词时起作用。
如果字符串包含两个或多个单词,如“卡片指南”,则即使这些单词与名称中的单词完全相同,也不会返回任何内容
我如何才能正确地执行此搜索
Rails 3 Ruby 1.9.2
更新
事实证明,这是一种Postgresql语法。我需要使用iLIKE来区分大小写。然后它工作得更好。我仍然喜欢下面的答案,因为它也有助于完善我的语法。嘿,我也在我的项目中使用这种类型的搜索逻辑,我是这样做的。如果你想尝试,那么看看它是否对你有帮助-:
class << self
def search(query)
product = "%#{query}%"
where("name LIKE ? AND locale =?", product,I18n.locale)
end
end
class我注意到您使用的是大写。您确定产品名称的格式与存储数据的格式完全相同吗?是否代码返回“卡片指南”,但您的数据需要“卡片指南”?谢谢Vivek,这帮了大忙