Forum: Ruby on Rails Working with an array of search parameters

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ali (Guest)
on 2007-08-01 19:38
(Received via mailing list)
Hi!

I have a search form where user may search by one or a combo of these
items:
Last Name
First Name
Alias Last Name
Alias First Name
Case Number

First of all, how do I set up the fields in _form to return an array
to the controller?

Secondly, how do I access the array in the controller and generate an
appropriate "find" statement based on what array elements were passed
in?

As it stands now, I'm setting each parameter to a variable and then in
a giant "if" statement constructing the "find" statement.  This worked
ok with just name and case number to search for.  Now that I've added
alias to the mix, this is turning into some majorly UGLY code.  Too
many possible combinations!

Any thoughts/suggestions/ideas are welcomed and appreciated.

Thank you!!  ~Ali
Mike G. (Guest)
on 2007-08-02 06:25
(Received via mailing list)
you can use the criteria_query plugin to make life a little easier:
http://agilewebdevelopment.com/plugins/criteria_query

I tried using it myself, but ended up falling back on multiple if
statements and building the conditions string and params array by
hand, since in pretty much every case, I need paginate my result set,
which criteria query won't let you do.

Here's some example code from my search action in one of my
controllers, to show you my approach:

http://pastie.caboo.se/84234

I stored each search parameter into an ivar so when the page is
redisplayed, the search terms will still be filled out in my search
form..

Adam
This topic is locked and can not be replied to.