I have an app that stores titles and lyrics. Some songs are in spanish and contain áccénts. I'm trying to write a search method where a user can enter a word without accents. For example, let's say the title has the word "papá" in it. If the user types in "papa", it should still find that song with that word in the title.
This is what I have so far. It handles wildcards and searches through 3 columns.
class Song < ApplicationRecord
def self.search(query)
return all unless query.present?
# for case insensitive search
like = Rails.env.production? ? 'ILIKE' : 'LIKE'
# for wildcards
q = ["%#{query}%"] * 3
# columns that I will search through song.number, song.title, song.lyrics
search_phrase = "number #{like} ? OR title #{like} ? OR lyrics #{like} ?"
where([search_phrase] + q)
end
end