LAB
2007-Aug-09 20:20 UTC
Using a Find statement with user input, not selecting the right table entries
I have a search form that takes user input, and displays the results in a search_results.rhtml view. In my controller for the search form, I have the following declarations and am using the call to "find": beds = params[:beds] baths = params[:baths] sqft = params[:sqft] neighborhood_id3 = params[:neighborhood_id3] neighborhood_id4 = params[:neighborhood_id4] neighborhood_id6 = params[:neighborhood_id6] @properties = Property.find(:all, :conditions => ["AREA like ? OR AREA like ? OR AREA like ? AND NO_BEDROOMS >= ? AND NO_FULL_BATHS >= ? AND SQUARE_FEET >= ?", neighborhood_id3, neighborhood_id4, neighborhood_id6, beds, baths, sqft]) Then I display all the properties returned in the search_results.rhtml view. The neighborhood part is working. These are checkboxes on the search form, and if you check neighborhood_id3, you get only that neighborhood returned in the result. However, the beds, baths, and sqft, are not working. The entries that are returned are not within what the user selected, ie: only show properties with beds > 2 does not work. Can anyone see what the problem is in my find statement? In my sql database, I changed the types to varchars, thinking that they had to be strings to do the comparison. Not sure if that is true, but it didn''t help me in any case. Maybe someone could suggest a more elegant implementation as well ... this does seem a little brute force? Thanks in advance! LAB --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
rbaldwin
2007-Aug-10 01:32 UTC
Re: Using a Find statement with user input, not selecting the right table entries
You need parenthesis around all of your or''d AREA conditions to get them to work properly with your and''d conditions: (AREA like ? OR AREA like ? OR AREA like ?) AND NO_BEDROOMS >= ? AND NO_FULL_BATHS >= ? AND SQUARE_FEET >= ? On Aug 9, 2:20 pm, LAB <LABus...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a search form that takes user input, and displays the results > in a search_results.rhtml view. In my controller for the search form, > I have the following declarations and am using the call to "find": > > beds = params[:beds] > baths = params[:baths] > sqft = params[:sqft] > neighborhood_id3 = params[:neighborhood_id3] > neighborhood_id4 = params[:neighborhood_id4] > neighborhood_id6 = params[:neighborhood_id6] > > @properties = Property.find(:all, :conditions => ["AREA like ? OR AREA > like ? OR AREA like ? AND NO_BEDROOMS >= ? AND NO_FULL_BATHS >= ? AND > SQUARE_FEET >= ?", neighborhood_id3, neighborhood_id4, > neighborhood_id6, beds, baths, sqft]) > > Then I display all the properties returned in the search_results.rhtml > view. > > The neighborhood part is working. These are checkboxes on the search > form, and if you check neighborhood_id3, you get only that > neighborhood returned in the result. However, the beds, baths, and > sqft, are not working. The entries that are returned are not within > what the user selected, ie: only show properties with beds > 2 does > not work. > > Can anyone see what the problem is in my find statement? In my sql > database, I changed the types to varchars, thinking that they had to > be strings to do the comparison. Not sure if that is true, but it > didn''t help me in any case. > > Maybe someone could suggest a more elegant implementation as well ... > this does seem a little brute force? > > Thanks in advance! > LAB--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
LAB
2007-Aug-10 14:54 UTC
Re: Using a Find statement with user input, not selecting the right table entries
The parenthesis were key, and I also needed to change the first AND to an OR. That gives me the results I was looking for. Thanks! On Aug 9, 9:32 pm, rbaldwin <ryan.bald...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> You need parenthesis around all of your or''d AREA conditions to get > them to work properly with your and''d conditions: > > (AREA like ? OR AREA like ? OR AREA like ?) AND NO_BEDROOMS >= ? AND > NO_FULL_BATHS >= ? AND SQUARE_FEET >= ? > > On Aug 9, 2:20 pm, LAB <LABus...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I have a search form that takes user input, and displays the results > > in a search_results.rhtml view. In my controller for the search form, > > I have the following declarations and am using the call to "find": > > > beds = params[:beds] > > baths = params[:baths] > > sqft = params[:sqft] > > neighborhood_id3 = params[:neighborhood_id3] > > neighborhood_id4 = params[:neighborhood_id4] > > neighborhood_id6 = params[:neighborhood_id6] > > > @properties = Property.find(:all, :conditions => ["AREA like ? OR AREA > > like ? OR AREA like ? AND NO_BEDROOMS >= ? AND NO_FULL_BATHS >= ? AND > > SQUARE_FEET >= ?", neighborhood_id3, neighborhood_id4, > > neighborhood_id6, beds, baths, sqft]) > > > Then I display all the properties returned in the search_results.rhtml > > view. > > > The neighborhood part is working. These are checkboxes on the search > > form, and if you check neighborhood_id3, you get only that > > neighborhood returned in the result. However, the beds, baths, and > > sqft, are not working. The entries that are returned are not within > > what the user selected, ie: only show properties with beds > 2 does > > not work. > > > Can anyone see what the problem is in my find statement? In my sql > > database, I changed the types to varchars, thinking that they had to > > be strings to do the comparison. Not sure if that is true, but it > > didn''t help me in any case. > > > Maybe someone could suggest a more elegant implementation as well ... > > this does seem a little brute force? > > > Thanks in advance! > > LAB--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---