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
--
Posted via http://www.ruby-forum.com/.
Hmmm...
Apparently, putting num as part of the string solves the problem.
arr2 += ["%#{arr[num]}%"]
Interesting.
-Gilles
--
Posted via http://www.ruby-forum.com/.
Hi Gilles,
I don''t know if things will work in a way that is performant enogh with
your
LIKE ''%...%'' query. But you could well solve performance
problems later.
IMHO you can''t split an #{expression}, it needs to be in an unsplit
String.
Why don''t you put it like
expr = arr[num]
arr2 += ["%#{expr}%"]
or on one line
arr2 += ["%" + arr[num] + "%"]
For complex queries like the ones you are in need of you might find a
fulltext-search-engine useful to rule out performance issues if you''ve
got
lots of records. Search for ''ferret'' and
''acts_as_ferret'' for example.
Cheers,
Jan
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060804/7aaafbea/attachment.html
Ah I see, you already found an even better way. Yes the expression of #{}
gets evaluated inside of the string.
Cheers,
Jan
On 8/4/06, Jan Prill <jan.prill@gmail.com> wrote:>
> Hi Gilles,
>
> I don''t know if things will work in a way that is performant enogh
with
> your LIKE ''%...%'' query. But you could well solve
performance problems
> later.
>
> IMHO you can''t split an #{expression}, it needs to be in an
unsplit
> String. Why don''t you put it like
> expr = arr[num]
> arr2 += ["%#{expr}%"]
>
> or on one line
>
> arr2 += ["%" + arr[num] + "%"]
>
> For complex queries like the ones you are in need of you might find a
> fulltext-search-engine useful to rule out performance issues if
you''ve got
> lots of records. Search for ''ferret'' and
''acts_as_ferret'' for example.
>
> Cheers,
> Jan
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060804/0f4a27dc/attachment.html
Jan Prill wrote:> Ah I see, you already found an even better way. Yes the expression of > #{} > gets evaluated inside of the string. > > Cheers, > JanAhhhh I see. Thanks for the reply Jan! -Gilles -- Posted via http://www.ruby-forum.com/.