Topic case insensitive search


(Keith John Hutchison) #1

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?


(Keith John Hutchison) #2

From User Gabbar - Stack Overflow
ruby on rails - Case-insensitive search within discourse console - Stack Overflow

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

(Sam Saffron) #3

Look at our search specs, they demonstrate how in:title search works.


(Keith John Hutchison) #4

Thanks, Sam

Does in:title mean case-insensitive searches within the title field?