[英]How to perform a case insensitive search in a Rails app?
在Rails 3.2應用程序中,我有一個搜索字段,該字段引用用戶模型中的以下例程。
def self.search(search)
if search
where('fullname LIKE ? OR email LIKE ?', "%#{search}%", "%#{search}%")
else
scoped
end
end
這似乎在執行區分大小寫的搜索。 如何將其修改為不區分大小寫?
非常感謝
如果您使用的是Postgres,則可以使用“ ilike”:
def self.search(search)
if search
where('fullname ILIKE ? OR email ILIKE ?', "%#{search}%", "%#{search}%")
else
scoped
end
end
過去完成此操作后,我使用了MySQL例程UPPER。 所以像
def self.search(search)
if search
where('UPPER(fullname) LIKE ? OR UPPER(email) LIKE ?', upcase(search), upcase(search))
else
scoped
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.