Find params

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

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 ?)’

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 ?

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.

On Thu, Feb 25, 2010 at 10:45 AM, Alex M. [email protected]
wrote:

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

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

Jesus.

using like a simple array worked like a charm, thanks a lot :smiley:

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.

Alex M. wrote:

using like a simple array worked like a charm, thanks a lot :smiley:

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.