# Using a Find statement with user input, not selecting the ri

**URL:** <https://www.ruby-forum.com/t/using-a-find-statement-with-user-input-not-selecting-the-ri/111701>\
**Category:** Rails\
**Created:** [August 9, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/using-a-find-statement-with-user-input-not-selecting-the-ri/111701 "2007-08-09T22:21:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![LAB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@LAB](https://www.ruby-forum.com/u/LAB)\
**Post date:** [August 9, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/using-a-find-statement-with-user-input-not-selecting-the-ri/111701/1 "2007-08-09T22:21:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LAB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@LAB](https://www.ruby-forum.com/u/LAB)\
**Post date:** [August 10, 2007, 3:33am UTC](https://www.ruby-forum.com/t/using-a-find-statement-with-user-input-not-selecting-the-ri/111701/2 "2007-08-10T03:33:25Z")

</div>

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 \>= ?

---

<div class="post-metadata">

**Author:** ![LAB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@LAB](https://www.ruby-forum.com/u/LAB)\
**Post date:** [August 10, 2007, 4:54pm UTC](https://www.ruby-forum.com/t/using-a-find-statement-with-user-input-not-selecting-the-ri/111701/3 "2007-08-10T16:54:52Z")

</div>

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!
