开发者

simple search in rails

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 countri开发者_如何学编程es in country_to

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


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)


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]])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜