Hi, I’m trying to implement a live search that supports searching for
multiple words. So for example, if I type “ruby rails” in the search
field, I would return results that have either the word “ruby” in them
or the word “rails” in them. However, this is just a background as to
what the following code is trying to do and might not even be needed to
fix my seemingly simple problem that I am unable to solve. I believe I
have the algorithm down to do what I want, below.
arr = @params[:query].split()
condition = @params[:attribute].to_s + " LIKE ?"
while num < arr.length
condition += "and " + @params[:attribute].to_s + " LIKE ?"
num += 1
end
arr2 = [condition, "%#{arr[0]}%"]
num = 1
while num < arr.length
arr2 += ["%#{arr[" + num + "]}%"] # <--- cannot convert String
into Integer
num += 1
end
At the noted line, I receive the error “cannot convert String into
Integer”. When I hardcode a that line to:
arr2 += ["%#{arr[1]}%"]
Everything works fine for only two word searches. So the num variable is
causing me problems for some reason. I perform both a num.to_s and a
num.to_i just to see if it’ll fix the issue or give me a different
error, but have not succeeded. This could be very simple and I am merely
not seeing what needs to be done, but in any case, I need help. Thanks!
-Gilles