# Cannot convert String into Integer

**URL:** <https://www.ruby-forum.com/t/cannot-convert-string-into-integer/101394>\
**Category:** Rails\
**Created:** [August 4, 2006, 5:31pm UTC](https://www.ruby-forum.com/t/cannot-convert-string-into-integer/101394 "2006-08-04T17:31:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 4, 2006, 5:31pm UTC](https://www.ruby-forum.com/t/cannot-convert-string-into-integer/101394/1 "2006-08-04T17:31:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 4, 2006, 5:41pm UTC](https://www.ruby-forum.com/t/cannot-convert-string-into-integer/101394/2 "2006-08-04T17:41:08Z")

</div>

Hmmm…

Apparently, putting num as part of the string solves the problem.

```
  arr2 += ["%#{arr[num]}%"]

```

Interesting.

-Gilles

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 4, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/cannot-convert-string-into-integer/101394/3 "2006-08-04T17:45:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 4, 2006, 5:48pm UTC](https://www.ruby-forum.com/t/cannot-convert-string-into-integer/101394/4 "2006-08-04T17:48:38Z")

</div>

Ah I see, you already found an even better way. Yes the expression of  
#{}  
gets evaluated inside of the string.

Cheers,  
Jan

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 4, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/cannot-convert-string-into-integer/101394/5 "2006-08-04T18:21:31Z")

</div>

Jan P. wrote:

> Ah I see, you already found an even better way. Yes the expression of  
> #{}  
> gets evaluated inside of the string.
> 
> Cheers,  
> Jan

Ahhhh I see. Thanks for the reply Jan!

-Gilles
