Search for multiple keywords in a single search text box (RAILS)

I am new to Rails and I am looking for databases. I have a model and a database that has a list of names under the "name" attribute. I want to be able to enter keywords in one search field, and this input can consist of one word or two words or more, depending on what specific result the user needs.

Right now, I'm using something ugly, as shown below, which will use no more than three search queries. Is there a way to make this dynamic for the keywords "search_length"? The find method is clearly repeated, but I'm not sure how to automate it and have not found any useful suggestions elsewhere on the Internet.

def self.search(search)

  if search
    search_length = search.split.length
    find(:all, :conditions => ['name LIKE ? AND name LIKE ? AND name LIKE ?', 
    "%#{search.split[0]}%", "%#{search.split[1]}%", 
    "%#{search.split[search_length1]}%"])
  else
    find(:all)
  end
end

Other than that, loving Rails so far.

,

+3
3

Łukasz Śliwa , %.

. .

def self.search(search)

  if search
    search_length = search.split.length
    find(:all, :conditions => [(['name LIKE ?'] * search_length).join(' AND ')] + search.split.map { |name| "%#{name}%" })
  else
    find(:all)
  end

end
+7

- :

find(:all, :conditions => [(['name LIKE ?'] * search_length).join(' AND ')] + search.split.map { |name| "%#{name}" })

, search_length times string 'name LIKE?':

 ['name LIKE ?'] * search_length

, "AND":

 ["name LIKE ? ", "name LIKE ? ", "name LIKE ? "].join(' AND ')

, , .

+2
formatted_columns = format_column_names(Sub.column_names)
where(formatted_columns.map {|cn| "#{cn} like ?" }.join("or "), *(["%#{search}%"] * formatted_columns.size))

,

0

All Articles