G''day I am a complete newbie to programming. All I have to go on at this point is the Agile book from Pragmatic Prog. and all the reading I have been doing on the net. I am looking (begging!) for some help with the following: I have a form that collects multiple parameters in a view template, as such: <form action="search" method="get"> <tr> <td> <span class="list-employee_number" > <input name "empno" type=text value = ""> </td> <td> <span class="list-employee_number" > <input name "title" type=text value = ""> </td> <td> <span class="list-employee_number" > <input name "givname" type=text value = ""> </td> <td> <span class="list-employee_number" > <input name "surname" type=text value = ""> </td> <td> <span class="list-employee_number" > <input type "submit" value = "Search" /> </tr> </form> The data entered by the user is then sent to the employee controller file which holds the following: def search empno = params[:empno] title = params[:title] givname = params[:givname] surname = params[:surname] @employees = Employee.find(:all,:conditions => ["employee_number ? and title = ? and given_name = ? and surname = ?", empno, title, givname, surname]) end This all works fine if the user enters a value into each one of the four fields in the form, but if one of the fields is left blank (eg given_name), I get zero results, even if the data entered into all fields other than given_name does match with an existing record. From my limited knowledge, this seems to be happening because the SQL query constructed tries to find a record where value = null for the field that was left blank (eg given_name). How can I amend the find method to only bring field conditions into the query construct if the user has entered values against those fields? I have spent a long time trying to figure this out, but just cannot seem to come up with a solution. Any help would be very much appreciated. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hassan Kani wrote:> G''day > > I am a complete newbie to programming. All I have to go on at this > point is the Agile book from Pragmatic Prog. and all the reading I have > been doing on the net. > > I am looking (begging!) for some help with the following: > > I have a form that collects multiple parameters in a view template, as > such: > > <form action="search" method="get"> > <tr> > <td> <span class="list-employee_number" > <input name > "empno" type=text value = ""> </td> > <td> <span class="list-employee_number" > <input name > "title" type=text value = ""> </td> > <td> <span class="list-employee_number" > <input name > "givname" type=text value = ""> </td> > <td> <span class="list-employee_number" > <input name > "surname" type=text value = ""> </td> > <td> <span class="list-employee_number" > <input type > "submit" value = "Search" /> > </tr> > </form> > > The data entered by the user is then sent to the employee controller > file which holds the following: > > def search > empno = params[:empno] > title = params[:title] > givname = params[:givname] > surname = params[:surname] > > @employees = Employee.find(:all,:conditions => ["employee_number > ? > and title = ? > and given_name = ? > and surname = ?", > empno, > title, > givname, > surname]) > end > > This all works fine if the user enters a value into each one of the four > fields in the form, but if one of the fields is left blank (eg > given_name), I get zero results, even if the data entered into all > fields other than given_name does match with an existing record. > > From my limited knowledge, this seems to be happening because the SQL > query constructed tries to find a record where value = null for the > field that was left blank (eg given_name). > > How can I amend the find method to only bring field conditions into the > query construct if the user has entered values against those fields?First off, you can use the new hash based conditions if you''re using Rails version 1.2.3 (I''m not sure when they were introduced). If you then change your input names in the view to match the column names (empno should be employee_number and givname should be given_name) you can simply do: Employee.find(:all, :conditions => params.reject{|key,value| value.nil?}) If you''re not using a recent version either update or ask again. -- Cheers, - Jacob --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Many thanks Jacob, your method is far simpler than what i was trying. Will try it as soon as I get out of here. ;) thanks again -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I seem to still be having problems with this one. My new code in the view now matches the correct column names. In the controller, I now have the following: def search employee_number = params[:employee_number] title = params[:title] given_name = params[:given_name] surname = params[:surname] Employee.find(:all, :conditions => params.reject{|key,value| value.nil?}) end If I leave all criteria blank in the search, I am now getting the error: ActiveRecord::StatementInvalid in EmployeesController#search Mysql::Error: #42S22Unknown column ''employees.action'' in ''where clause'': SELECT * FROM employees WHERE (employees.`action` = ''search'' AND employees.`controller` = ''employees'') I''m sure I have done something wrong, but just cannot see what. ps: am using rails version 1.2.3. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Well ... the params hash also contains a key/value pair like this: params[:action] => "search" it''s from the <form action=search ...> you should put all search params in a su-hash of params, example: <input name = "searach[employee_number]" type=text value = ""> </td> this will then be available in params[:search][:employee_number] etc.pp. controller then looks like this: def search Employee.find(:all, :conditions => params[:search].reject{|key,value| value.nil?}) end hope that really works ;) On 5 Jun., 12:59, Complete Newbie <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I seem to still be having problems with this one. > > My new code in the view now matches the correct column names. In the > controller, I now have the following: > > def search > employee_number = params[:employee_number] > title = params[:title] > given_name = params[:given_name] > surname = params[:surname] > > Employee.find(:all, > :conditions => params.reject{|key,value| value.nil?}) > end > > If I leave all criteria blank in the search, I am now getting the error: > > ActiveRecord::StatementInvalid in EmployeesController#search > Mysql::Error: #42S22Unknown column ''employees.action'' in ''where clause'': > SELECT * FROM employees WHERE (employees.`action` = ''search'' AND > employees.`controller` = ''employees'') > > I''m sure I have done something wrong, but just cannot see what. > > ps: am using rails version 1.2.3. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the help...but now I get: You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occurred while evaluating nil.reject when i try to submit the form with just a single parameter. Sorry, I know it must be a really simple error...bit lost here. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
That error is most likely due to the Employee.find returning no results, therefore @employee => nil. check your development.log what the SQL looks like that is queried, check in the controller with if @employee != nil etc.... what does the params look like on your error page? are they as expected? (shown at the bottom) On 5 Jun., 13:34, Complete Newbie <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Thanks for the help...but now I get: > > You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occurred while evaluating nil.reject > > when i try to submit the form with just a single parameter. > > Sorry, I know it must be a really simple error...bit lost here. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Here''s what I can see. I do a search using just employee number (and the number I know does exist, being 1123). So... In the development log: Parameters: {"search"=>{"title"=>nil, "given_name"=>nil, "employee_number"=>"1123", "surname"=>nil}, "action"=>"search", "controller"=>"employees"} [4;36;1mEmployee Load (0.000000) [0;1mSELECT * FROM employees WHERE (employees.`employee_number` = ''1123'') Looks like the SLQ query itself is OK. So far, so good. On the error page, it says I have a problem with the line that reads: <% for employee in @employees %> And on the bottom of that error page, the parameters are shown as: Parameters: {"search"=>{"title"=>nil, "given_name"=>"Jason", "employee_number"=>nil, "surname"=>nil}} I could understand if no records were matched that this error may come about, but the record does actually exist. I also tried doing a search using just one of the other fields and get the same results... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
just figured it out! all working now...many thanks for all the help, much appreciated. cheers ;) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
so what was the error? On 5 Jun., 14:51, Complete Newbie <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> just figured it out! all working now...many thanks for all the help, > much appreciated. > > cheers ;) > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I had: def search Employee.find(:all, :conditions => params[:search].reject{|key,value| value.nil?}) end when I should have had: def search @employees = Employee.find(:all, :conditions => params[:search].reject{|key,value| value.nil?}) end I''m now onto my next mission of trying to figure out how to allow LIKE search using this approach (eg. looking for employees by name must cater for slight mis-spellings of names)...time for another coffee! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---