Hi there,
I'm starting to learn Rails, and I have a question about SQL usage.
I have some fields on my view that will be used as a search form.
So, I have the search parameters, but what's the best way to
use them on a sql query?
let's say I have 3 search parameters, and the user uses 2 of them.
Object.find(:condition => ["text LIKE ? AND text2 = ? AND number 3 =
?","bla","ble",3])
I can't do this, because if the user send me only 2 parameters the query
wont work.
You can pretty easily adapt your controller to the number of parameters
the user returns from their data entry... just construct your condition
according to the data you received.
Also, wrap your values in '%X%' instead of just 'X' so that you get
substring matches.
I second the idea of using ferret or solr. You get all sorts of
benefits with those: better indexing, faster searching, multi-field,
and/or selectors, and more. The only reason I'd roll my own is if my
needs were really constrained. In which case, what Julian posted would
work very well.
Thanks for all the help!
I have tried ferret, but i had problems with relationed tables.
And as I could see, ferret is for text search only, right?
I could't get to do numerical searches like x > 10 or something like
that.