# Newbie stuck with multiple conditions

**URL:** <https://www.ruby-forum.com/t/newbie-stuck-with-multiple-conditions/119901>\
**Category:** Ruby\
**Created:** [November 12, 2007, 1:47am UTC](https://www.ruby-forum.com/t/newbie-stuck-with-multiple-conditions/119901 "2007-11-12T01:47:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@martinr](https://www.ruby-forum.com/u/martinr)\
**Post date:** [November 12, 2007, 1:47am UTC](https://www.ruby-forum.com/t/newbie-stuck-with-multiple-conditions/119901/1 "2007-11-12T01:47:49Z")

</div>

Hi

I am new to rails and programming.

I am trying to get a conditional search from form results for a property  
search.

I want to be able to contruct a search that will search (:all) if ID for  
all is returned or by individual ID

The code below does not work for the all condition

def results

suburb\_id=params[:suburb][:id]  
bedroom\_id=params[:bedroom][:id]  
garage\_id=params[:garage][:id]  
@listings=ListingDetail.find(  
:all,  
:conditions =\>[“suburb\_id =:suburb\_id and bedroom\_id=:bedroom\_id and  
garage\_id=:garage\_id”,  
{  
:suburb\_id =\>suburb\_id,  
:bedroom\_id=\>bedroom\_id,  
:garage\_id=\>garage\_id  
}  
]  
)  
end

I have also tried use case and if as below

def results

bedroom\_id=params[:bedroom][:id]  
case bedroom\_id  
when 6  
@listings=ListingDetail.find\_all\_by\_bedroom\_id(:all)  
else  
@listings=ListingDetail.find\_all\_by\_bedroom\_id(bedroom\_id)  
end  
garage\_id=params[:garage][:id]  
case garage\_id  
when 4  
@listings=ListingDetail.find\_all\_by\_garage\_id(:all)  
else  
@listings=ListingDetail.find\_all\_by\_garage\_id(garage\_id)  
end

end

Any help would be appreciated

Martin

---

<div class="post-metadata">

**Author:** ![martinr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@martinr](https://www.ruby-forum.com/u/martinr)\
**Post date:** [November 12, 2007, 4:11am UTC](https://www.ruby-forum.com/t/newbie-stuck-with-multiple-conditions/119901/2 "2007-11-12T04:11:52Z")

</div>

1. You should be posting to the rails list, not the ruby list.

2. Conditions takes an array OR a dictionary. I believe what you are  
trying to do is the following:

@listings = ListingDetail.find(:all, :conditions = [“suburb\_id = ? AND  
bedroom\_id = ? AND garage\_id = ?”, params[:suburb\_id], params  
[:bedroom\_id], params[:garage\_id]])

1. There is a logic error in your second case, calling  
type\_find\_by\_other\_id, you need to pass an actual id. :all means find  
all. If you want to find all, its just find(:all).

And for your object model, I’m thinking a listing should have a int  
number rooms and either int number car garage, or bool if has garage.

Sorry if this isn’t more informative, but I’m on the bus and its bumpy.

Jake

Sent from my iPhone

On Nov 11, 2007, at 6:47 PM, Martin Robertson

---

<div class="post-metadata">

**Author:** ![martinr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@martinr](https://www.ruby-forum.com/u/martinr)\
**Post date:** [November 12, 2007, 5:38am UTC](https://www.ruby-forum.com/t/newbie-stuck-with-multiple-conditions/119901/3 "2007-11-12T05:38:01Z")

</div>

Hi Jacob

I have tried what you have suggested

@listings = ListingDetail.find(:all, :conditions =\> [“suburb\_id = ? AND  
bedroom\_id = ? AND garage\_id = ?”, params[:suburb\_id], params  
[:bedroom\_id], params[:garage\_id]])

but it throws up errors as

app/controllers/admin/listing\_details\_controller.rb:55: syntax error,  
unexpected tLBRACK, expecting ‘]’  
[:bedroom\_id], params[:garage\_id]])  
^  
app/controllers/admin/listing\_details\_controller.rb:55: syntax error,  
unexpected ‘,’, expecting kEND  
[:bedroom\_id], params[:garage\_id]])  
^  
app/controllers/admin/listing\_details\_controller.rb:55: syntax error,  
unexpected ‘]’, expecting kEND  
[:bedroom\_id], params[:garage\_id]])  
^  
app/controllers/admin/listing\_details\_controller.rb:85: syntax error,  
unexpected $end, expecting kEND

I have also tried a variation of this

def results

suburb\_id=params[:suburb][:id]  
bedroom\_id=params[:bedroom][:id]  
garage\_id=params[:garage][:id]  
@listings=ListingDetail.find(  
:all,  
:conditions =\>[“suburb\_id =:suburb\_id and bedroom\_id=:bedroom\_id and  
garage\_id=:garage\_id”,  
{  
:suburb\_id =\>suburb\_id,  
:bedroom\_id=\>bedroom\_id,  
:garage\_id=\>garage\_id  
}  
]  
)  
end

this produces error message

Showing app/views/admin/listing\_details/results.rhtml where line #2  
raised:

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.each

Extracted source (around line #2):

1:

  
2: \<% for listings in @listings %\>  
3: - \<%=listings.descriptionthumb %\>
  
4: \<% end %\>  
5: 

RAILS\_ROOT: ./script/…/config/…

(–deleted all this stuff–)

Request  
Parameters: {“garage”=\>{“id”=\>“1”}, “commit”=\>“Search”,  
“suburb”=\>{“id”=\>“2”}, “bedroom”=\>{“id”=\>“1”}}

I have also posted this as new post in correct rails forum as advised

Martin

---

<div class="post-metadata">

**Author:** ![martinr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@martinr](https://www.ruby-forum.com/u/martinr)\
**Post date:** [November 12, 2007, 10:28pm UTC](https://www.ruby-forum.com/t/newbie-stuck-with-multiple-conditions/119901/4 "2007-11-12T22:28:37Z")

</div>

Martin,

Your “variation” is doing the same as what you had before in your  
first email. Also creating variables with your parameters is not  
necessary, pointless really. And yes, what I put was a bit off, as I  
said I was on the bus. I believe you can go

@listings = ListingDetail.find(:all, :conditions =\> {:garage\_id =\>  
params[:garage][:id], :room\_id =\> params[:room][:id], :suburb\_id =\>  
params[:suburb][:id] }

What this is doing is creating a Hash of their key/value pairs, and  
passing that Hash as the conditions for the find query. If you look at  
the console window you are running the rails server in, you should see  
something like

SELECT \* FROM ListingDetails WHERE garage\_id = x AND room\_id = y AND  
suburb\_id = z

Now, you need to be careful because you will get into some tricky  
business if either of your three id’s are not passed in your search.  
That should give you enough to get running ,but like I said before  
this is really something that should be discussed on the rails mailing  
list.

Good Luck,  
Jake
