From: Tom Ha Date: 2008-03-08T19:39:48+09:00 Subject: Creating a search Hi there, I just spent a night on the following "problem" and cant' figure it out on my own: I need to have a search function and thought I could do it like this: - the search page has a form which takes the search criteria (people only fill in the criteria they want to use for the search) - upon submission of the form, the search criteria goes into params[:search] (i.e. params[:search][:firstname], params[:search][:lastname], params[:search][:gender], etc.) - the controller takes the values from params[:search] and should generate/write the appropriate "conditions" for the search statement "User.find(:all, conditions => {...})" - But since submitting the search page/form with only *some* (not all) fields filled in (or check boxes checked, or radio buttons clicked, etc.) would generate some "empty" values in the key/value pairs in "params[:search]", the generated search statement would be wrong because for example a condition saying " 'gender' => nil/empty " could be added. - So, only key/value pairs which have a value that's not empty should generate the "condition" code. Question 1: Where is the mistake in my code? If I do it like below, I get the error: "odd number list for Hash" (at line: see the comment in the code). (Note: in this code I don't exclude the "empty" values.) Question 2: How do I exclude key/value pairs with an "empty" value from generating a "condition" (that would screw my search statement)? The code until now looks like this: def search @searchresults = '' if request.post? @searchresults = User.find(:all, :conditions => { # this should render like: 'firstname' => params[:search][:firstname], # and no comma for the last: 'gender' => params[:search][:gender] i=0 # <= I get the error for this line params[:search].each do |key, value| i+=1 if i < params[:search].length puts "'"+key+"'"+" => params[:search][:"+key+"]," else puts "'"+key+"'"+" => params[:search][:"+key+"]" end end }) end end I spent so many hours thinking about this that I would be really thankful for a working solution! Kind regards, Tom -- Posted via http://www.ruby-forum.com/.