ruby-on-railsdiscourse

Case-insensitive search within discourse console


I discovered that using

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

will always return nil as the topic title when saved becomes Lowercase and More

I tried using

topics = Topic.find_by_sql("select * from topics where lower(title) = '#{title.gsub(/'/,"''").downcase}'")
if topics.length > 0 then
    result = topics[0]
end

which resolves title: 'lowercase and More' but it brings up other edge cases.

Is there a simple way to do a case-insensitive searches within discourse using the rails console?


Solution

  • Add a validation in model

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

    Query

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