Dynamic Find using LIKE

Brandon S. wrote:

Hi all,

I have a simple text box for the user to enter a search term. I think
there is a helper that will convert a search term like "word_1 word_2"
into a generated SQL query like this:

SELECT * FROM table WHERE term LIKE '%word_1%' OR term LIKE '%word_2%'

Does a helper method like this exist?

Brandon

Something like this might do it:

terms = []
params[:search_term].split(' ').each {|t| terms.push('%' + t + '%')}

Don't forget to escape 't' above using whatever the db-escape method is called...