# Find params

**URL:** <https://www.ruby-forum.com/t/find-params/184111>\
**Category:** Ruby\
**Created:** [February 25, 2010, 10:05am UTC](https://www.ruby-forum.com/t/find-params/184111 "2010-02-25T10:05:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [February 25, 2010, 10:05am UTC](https://www.ruby-forum.com/t/find-params/184111/1 "2010-02-25T10:05:34Z")

</div>

Hi all,

im new on ruby and im experimentig with find method. I’ve made a “Ã±apa”  
as we say in Spain to solve a problem, that detects the params and  
creates the params for find:

```
------------ CODE ----------------------
if(cont==1)
  hash = [cad,"%#{queryArray[0]}%"]
elsif(cont==2)
  hash = [cad,"%#{queryArray[0]}%","%#{queryArray[1]}%"]
[......]

```

cad = ‘title OR description LIKE ? AND title OR description like ?’

@products = Product.find(:all, :conditions =\> hash)  
------------ CODE ----------------------

obviously the conditional part is very ugly, but iÂ´ve been looking to  
build it dinamically with no success. This is the content which it  
should be desirable to autogenerate deppendig on queryArray size:

[‘title OR description LIKE ? AND title OR description like ?’,  
“%#{queryArray[0]}%”, “%#{“queryArray[1]”}%”, “%#{“queryArray[2]”}%”,  
… ]

Is it possible to make it in ruby? Thanks a lot in advance

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [February 25, 2010, 10:33am UTC](https://www.ruby-forum.com/t/find-params/184111/2 "2010-02-25T10:33:10Z")

</div>

Alex M. wrote:

> [‘title OR description LIKE ? AND title OR description like ?’,  
> “%#{queryArray[0]}%”, “%#{“queryArray[1]”}%”, “%#{“queryArray[2]”}%”,  
> … ]

[’…’,  
queryArray.map { |e| “%#{e}%” }]

Are you sure your SQL syntax is correct though? I would have thought it  
should be:

‘(title LIKE ? OR description LIKE ?) AND (title LIKE ? OR description  
LIKE ?)’

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [February 25, 2010, 10:45am UTC](https://www.ruby-forum.com/t/find-params/184111/3 "2010-02-25T10:45:50Z")

</div>

Thanks a lot for your answer.

The problem is that map doesnÂ´t add commas, i get:

title OR description LIKE ? AND title OR description LIKE ?  
%pullmantur%%test%

and i need

title OR description LIKE ? AND title OR description LIKE ?  
%pullmantur%,%test%

so i get this answer for more than 1 parameter:

wrong number of bind variables (1 for 2) in: title OR description LIKE  
? AND title OR description LIKE ?

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [March 1, 2010, 3:18pm UTC](https://www.ruby-forum.com/t/find-params/184111/4 "2010-03-01T15:18:32Z")

</div>

Alex M. wrote:

> Thanks a lot for your answer.
> 
> The problem is that map doesnÂ´t add commas, i get:
> 
> title OR description LIKE ? AND title OR description LIKE ?  
> %pullmantur%%test%
> 
> and i need
> 
> title OR description LIKE ? AND title OR description LIKE ?  
> %pullmantur%,%test%
> 
> so i get this answer for more than 1 parameter:
> 
> wrong number of bind variables (1 for 2) in: title OR description LIKE  
> ? AND title OR description LIKE ?

If this is ActiveRecord then you don’t want commas, you want an array  
like this:

[“title OR description LIKE ? AND title OR description LIKE ?”,  
“%pullmantur%”, “%test%”]

because each ? pulls the next value out of the array.

So if you’re getting only one bind variable instead of 2, it’s because  
your queryArray has 1 element instead of 2.

But if you’re using a different DB access framework then YMMV.

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [March 1, 2010, 12:30pm UTC](https://www.ruby-forum.com/t/find-params/184111/5 "2010-03-01T12:30:52Z")

</div>

On Thu, Feb 25, 2010 at 10:45 AM, Alex M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> title OR description LIKE ? AND title OR description LIKE ?  
> %pullmantur%,%test%

queryArray.map { |e| “%#{e}%” }.join(“,”)

Jesus.

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [March 1, 2010, 8:45pm UTC](https://www.ruby-forum.com/t/find-params/184111/6 "2010-03-01T20:45:48Z")

</div>

using like a simple array worked like a charm, thanks a lot 😃

I can’t believe it was so simple :-):

```
cont = 0
queryArray.each{ |q|
  cadarray.concat( ["%#{queryArray[cont]}%"] )
  cont +=1
}

```

Thanks again.

Brian C. wrote:

> Alex M. wrote:
> 
> > Thanks a lot for your answer.
> > 
> > The problem is that map doesnÂ´t add commas, i get:
> > 
> > title OR description LIKE ? AND title OR description LIKE ?  
> > %pullmantur%%test%
> > 
> > and i need
> > 
> > title OR description LIKE ? AND title OR description LIKE ?  
> > %pullmantur%,%test%
> > 
> > so i get this answer for more than 1 parameter:
> > 
> > wrong number of bind variables (1 for 2) in: title OR description LIKE  
> > ? AND title OR description LIKE ?
> 
> If this is ActiveRecord then you don’t want commas, you want an array  
> like this:
> 
> [“title OR description LIKE ? AND title OR description LIKE ?”,  
> “%pullmantur%”, “%test%”]
> 
> because each ? pulls the next value out of the array.
> 
> So if you’re getting only one bind variable instead of 2, it’s because  
> your queryArray has 1 element instead of 2.
> 
> But if you’re using a different DB access framework then YMMV.

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [March 1, 2010, 9:37pm UTC](https://www.ruby-forum.com/t/find-params/184111/7 "2010-03-01T21:37:29Z")

</div>

Alex M. wrote:

> using like a simple array worked like a charm, thanks a lot 😃
> 
> I can’t believe it was so simple :-):
> 
> ```
> cont = 0
> queryArray.each{ |q|
> cadarray.concat( ["%#{queryArray[cont]}%"] )
> cont +=1
> }
> 
> ```
> 
> Thanks again.

No problem. Now, the original posting asked how to make it less ugly.  
The above could be replaced with a single line:

cadarray += queryArray.map { |e| “%#{e}%” }

or

cadarray.concat queryArray.map { |e| “%#{e}%” }

HTH,

Brian.
