【问题标题】:Case-insensitive search within discourse console话语控制台中不区分大小写的搜索
【发布时间】:2019-01-03 04:14:04
【问题描述】:

我发现使用

title = 'lowercase and More'
topic = Topic.find_by(title:title)

在保存为Lowercase and More时将始终返回nil作为主题标题

我尝试过使用

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 控制台在话语中进行不区分大小写的搜索?

【问题讨论】:

    标签: ruby-on-rails discourse


    【解决方案1】:

    在模型中添加验证

    validates :title, presence: true, uniqueness: {case_sensitive: false}
    

    查询

    title = 'lowercase and More'
    result = Topic.where('lower(title) = ?', title.downcase).first
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多