If you can change the database , you can and should use the solution provided by gjb.
Here is a solution in which you do not need to modify the database. Just collect all the possible first / last name pairs that you can get from the search box. Some code:
def name_pairs(string)
return nil unless string =~ /^\w+(\s+\w+)+$/
words = string.split(/\s+/)
result = []
1.upto(words.size-1) {|n| result << [words[0...n], words[n..-1]]}
result.collect {|f| f.collect {|nm| nm.join(" ")}}
end
, or. :
=> [["Jon", "Bon Jovi"], ["Jon", "Bon Jovi"]]
=> [["John", "Bongiovi"]]
=> nil
, ( , ) , , , . String , "Jon Bon Jovi".name_pairs.