Case-insensitive 在话语控制台中搜索
Case-insensitive search within discourse console
我发现使用
title = 'lowercase and More'
topic = Topic.find_by(title:title)
将始终 return nil 作为保存时的主题标题变为 Lowercase and More
我试过使用
topics = Topic.find_by_sql("select * from topics where lower(title) = '#{title.gsub(/'/,"''").downcase}'")
if topics.length > 0 then
result = topics[0]
end
这解决了 title: 'lowercase and More'
但它引发了其他边缘情况。
是否有使用 rails 控制台在话语中进行 case-insensitive 搜索的简单方法?
在模型中添加验证
validates :title, presence: true, uniqueness: {case_sensitive: false}
查询
title = 'lowercase and More'
result = Topic.where('lower(title) = ?', title.downcase).first
我发现使用
title = 'lowercase and More'
topic = Topic.find_by(title:title)
将始终 return nil 作为保存时的主题标题变为 Lowercase and More
我试过使用
topics = Topic.find_by_sql("select * from topics where lower(title) = '#{title.gsub(/'/,"''").downcase}'")
if topics.length > 0 then
result = topics[0]
end
这解决了 title: 'lowercase and More'
但它引发了其他边缘情况。
是否有使用 rails 控制台在话语中进行 case-insensitive 搜索的简单方法?
在模型中添加验证
validates :title, presence: true, uniqueness: {case_sensitive: false}
查询
title = 'lowercase and More'
result = Topic.where('lower(title) = ?', title.downcase).first