views:

45

answers:

2

Hi,

I'm making a simple search form in rails. In my search view I have two select boxes with fixed values like:

SELECT BOX 1          SELECT BOX 2
ALL,                  ALL,
FR,                   FR,
US,                   US,
DE                    DE

And I have 2 fields in my DB with country_from and country_to.

So for making a simple search like from FR to US I use:

@search_result = Load.find(:all, :conditions => "country_from='#{params[:country_from]}' AND country_to='#{params[:country_to]}'" )

that is fine, but I need to implement the ALL option as well, so when I make a search like from DE to ALL I get a list with all countries in country_to

I image I can do it with ifs...but what would be the most efficient way to do it?

+1  A: 

if i understood it correctly, it would be

@search_result = Load.find(:all, :conditions => ["country_from = ? AND country_to IN (?)", params[:country_from], params[:country_to]])
j.
That works in the first case when I have a fixed country from and to value. But when I select ALL it needs to query all from the table. For example from DE to ALL, means query where country_from ='DE'.
Adnan
if i understood what you want, this works for you. this search will return, for example, every `Load` with `country_from = DE` and `country_to = [FR, US, DE]`.
j.
+1  A: 

This is what you do:

cond = {}
cond[:country_from] = params[:country_from] unless params[:country_from] == "AL"
cond[:country_to] = params[:country_to] unless params[:country_to] == "AL"

@search_result = Load.all(:conditions => cond)
KandadaBoggu