find params

A

Alex Moreno

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
 
B

Brian Candler

Alex said:
['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 ?)'
 
A

Alex Moreno

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 ?
 
J

Jesús Gabriel y Galán

Thanks a lot for your answer.

The problem is that map doesn=B4t 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%

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

Jesus.
 
B

Brian Candler

Alex said:
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.
 
A

Alex Moreno

using like a simple array worked like a charm, thanks a lot :-D

I can't believe it was so simple :):


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

Thanks again.


Brian said:
Alex said:
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.
 
B

Brian Candler

Alex said:
using like a simple array worked like a charm, thanks a lot :-D

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top