Hello Everybody,
I am have an advance search form in which i have a field called :name
in which user can enter 1 or multiple words.
When there is more than 1 word, i am splitting that string and
collecting it in to an array.
@collect = Array.new @collect << name.split( )
this gives me an array [“John”, “whatever”, “anyother”]
how can i find each item in the array in my rails app?
how can i find each item in the array in my rails app?
This isn’t specific enough to know for sure what you want, but I’m going
to assume that you want to search a specific model on a specific field
for each of those terms… if it was me, I would do it like this.
Assume a model of Widget that has a ‘title’ string field…
in my controller action…
with params[:name] being passed in via the form…
def search @widgets = Widget.all(:conditions => [“title IN (?)”,
params[:name].split(/[ ,]/)])
end
That would let you type in “John, whatever anyother” and have it work
both on spaces and commas. It’s not very efficient though as it’s an exact match so If you have a widget named ‘Jon’ it won’t find it.
You may want to look into sphinx or whatever full text search options
your database provides…
-philip
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.