# Search conditons

**URL:** <https://www.ruby-forum.com/t/search-conditons/119914>\
**Category:** Rails\
**Created:** [November 12, 2007, 5:24am UTC](https://www.ruby-forum.com/t/search-conditons/119914 "2007-11-12T05:24:25Z")\
**Posts on this page:** 8\
**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, 5:24am UTC](https://www.ruby-forum.com/t/search-conditons/119914/1 "2007-11-12T05:24:25Z")

</div>

Hi

I am new to rails and programming and I want to contruct a search for  
three things.

suburb  
bedrooms  
garage

I have tried various code without success  
example  
def results  
@listings=ListingDetail.find(  
:all,  
:conditions =\>[“suburb\_id = ? and bedroom\_id= ? and garage\_id=?”,  
params[:suburb][:id],  
params[:bedroom][:id],  
params[:garage][:id]  
]  
)  
end

the above code does not seem to cater the fact that Id 1 wants all the  
results to be searched

suburb\_id name  
1 all  
2 aeroglen  
3 whitfield  
4 edehill

If ID 1 is returned the search all suburbs  
else  
suburb only

I also tried  
def results  
suburb\_id=params[:suburb][:id]  
if suburb\_id==1  
@listings=ListingDetail.find\_by\_suburb\_id(:all)  
else  
@listings=ListingDetail.find\_all\_by\_suburb\_id(suburb\_id)  
end

bedroom\_id=params[:bedroom][:id]  
if bedroom\_id==1  
@listings=ListingDetail.find\_by\_bedroom\_id(:all)  
else  
@listings=ListingDetail.find\_all\_by\_bedroom\_id(bedroom\_id)  
end

```
 garage_id=params[:garage][:id]

```

if garage\_id==1  
@listings=ListingDetail.find\_by\_garage\_id(:all)  
else  
@listings=ListingDetail.find\_all\_by\_bedroom\_id(garage\_id)  
end  
end

The above produces the following error

undefined method `each’ for #ListingDetail:0x46e89e0

The code in the results.rhtml is

1:

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

Any ideas?

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, 6:03am UTC](https://www.ruby-forum.com/t/search-conditons/119914/2 "2007-11-12T06:03:28Z")

</div>

you know how to use breakpoint . If yes then try using breakpoint like  
following.Also what is :all its not a variable rt?  
def results  
suburb\_id=params[:suburb][:id]  
if suburb\_id==1  
@listings=ListingDetail.find\_by\_suburb\_id(:all)  
else  
@listings=ListingDetail.find\_all\_by\_suburb\_id(suburb\_id)  
end

bedroom\_id=params[:bedroom][:id]  
if bedroom\_id==1  
@listings=ListingDetail.find\_by\_bedroom\_id(:all)  
else  
@listings=ListingDetail.find\_all\_by\_bedroom\_id(bedroom\_id)  
end

```
 garage_id=params[:garage][:id]

```

if garage\_id==1  
@listings=ListingDetail.find\_by\_garage\_id(:all)  
else  
@listings=ListingDetail.find\_all\_by\_bedroom\_id(garage\_id)  
end  
breakpoint @listings  
end

---

<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, 7:01am UTC](https://www.ruby-forum.com/t/search-conditons/119914/3 "2007-11-12T07:01:00Z")

</div>

Hi Abhi

I tried the code again with breakpointer

C:\web\week5\property\>ruby script/breakpointer  
Executing break point [#\<ListingDetail:0x47906a4  
@attributes={“city”=\>“Cairns”,  
“address1”=\>“111”, “status”=\>“New Listing”, “pic1”=\>nil,  
“address2”=\>“Reservoir  
Rd”, “pic2”=\>nil, “bath”=\>nil, “postcode”=\>“4978”, “price”=\>“199000”,  
“pic3”=\>nil, “study”=\>nil, “suburb\_id”=\>“21”, “pic4”=\>nil,  
“lounge”=\>nil, “descriptionfull”=\>nil, “descriptionthumb”=\>"1 Bedroom  
House ", “id”=\>“4”, “area”=\>nil, “pricety  
pe”=\>“Fixed Price”, “house\_type”=\>nil, “user\_id”=\>nil, “garage\_id”=\>“1”,  
“bedroo  
m\_id”=\>“1”, “state”=\>nil, “pool”=\>nil}\>] at  
./script/…/config/…/app/controller  
s/admin/listing\_details\_controller.rb:74 in `results’  
irb(#Admin::ListingDetailsController:0x47c7e38):001:0\>

also in the server i found this

Processing ListingDetailsController#results (for 127.0.0.1 at 2007-11-12  
15:49:59) [POST]  
Session ID: 19b7d1ef945a6956a1467df97540cf1c  
Parameters: {“garage”=\>{“id”=\>“1”}, “commit”=\>“Search”,  
“action”=\>“results”, “suburb”=\>{“id”=\>“1”}, “bedroom”=\>{“id”=\>“1”},  
“controller”=\>“admin/listing\_details”}  
â†[4;35;1mListingDetail Columns (0.000000)â†[0m â†[0mSHOW FIELDS FROM  
listing\_detailsâ†[0m  
â†[4;36;1mListingDetail Load (0.000000)â†[0m â†[0;1mSELECT \* FROM  
listing\_details WHERE (listing\_details.`suburb_id` = ‘1’) â†[0m  
â†[4;35;1mListingDetail Load (0.000000)â†[0m â†[0mSELECT \* FROM  
listing\_details WHERE (listing\_details.`bedroom_id` = ‘1’) â†[0m  
â†[4;36;1mListingDetail Load (0.000000)â†[0m â†[0;1mSELECT \* FROM  
listing\_details WHERE (listing\_details.`bedroom_id` = ‘1’) â†[0m  
etails.`bedroom_id` = ‘1’) â†[0m

What i dont understand is why does it not seach for all records in  
suburb as it has the paramter of 1 returned.

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, 8:19am UTC](https://www.ruby-forum.com/t/search-conditons/119914/4 "2007-11-12T08:19:59Z")

</div>

not quite sure i follow what you mean by

ListingDetail.find\_by\_garage\_id(:all,:conditions[query])

I tried

garage\_id=params[:garage][:id]  
if garage\_id==1  
@listings=ListingDetail.find\_by\_garage\_id(:all,:conditions[query])

else  
@listings=ListingDetail.find\_all\_by\_bedroom\_id(garage\_id)

with no success

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, 8:34am UTC](https://www.ruby-forum.com/t/search-conditons/119914/5 "2007-11-12T08:34:05Z")

</div>

This is not the way  
first of all tell me  
do you wish to have search result which satisfies all the three  
condition or  
do you wish to have search result that satisfies any of the three  
conditions

---

<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, 8:35am UTC](https://www.ruby-forum.com/t/search-conditons/119914/6 "2007-11-12T08:35:59Z")

</div>

Abhi M. wrote:

> This is not the way  
> first of all tell me  
> do you wish to have search result which satisfies all the three  
> condition or  
> do you wish to have search result that satisfies any of the three  
> conditions

here this  
def results  
@listings=ListingDetail.find(  
:all,  
:conditions =\>[“suburb\_id = ? and bedroom\_id= ? and garage\_id=?”,  
params[:suburb][:id],  
params[:bedroom][:id],  
params[:garage][:id]  
]  
)  
end  
means that you wish to have search result which satisfies all the three  
condition

and this  
def results  
suburb\_id=params[:suburb][:id]  
if suburb\_id==1  
@listings=ListingDetail.find\_by\_suburb\_id(:all)  
else  
@listings=ListingDetail.find\_all\_by\_suburb\_id(suburb\_id)  
end

bedroom\_id=params[:bedroom][:id]  
if bedroom\_id==1  
@listings=ListingDetail.find\_by\_bedroom\_id(:all)  
else  
@listings=ListingDetail.find\_all\_by\_bedroom\_id(bedroom\_id)  
end

```
 garage_id=params[:garage][:id]

```

if garage\_id==1  
@listings=ListingDetail.find\_by\_garage\_id(:all)  
else  
@listings=ListingDetail.find\_all\_by\_bedroom\_id(garage\_id)  
end

end  
means that you wish to have search result that satisfies any of the  
three conditions

---

<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, 8:44am UTC](https://www.ruby-forum.com/t/search-conditons/119914/7 "2007-11-12T08:44:31Z")

</div>

there is another way also  
you have to initialize an array  
def results  
@lists = Array.new  
suburb\_id=params[:suburb][:id]  
if suburb\_id==1  
@listings=ListingDetail.find\_by\_suburb\_id(:all)  
else  
@listings=ListingDetail.find\_all\_by\_suburb\_id(suburb\_id)  
end  
@lists \<\<@listings  
bedroom\_id=params[:bedroom][:id]  
if bedroom\_id==1  
@listings=ListingDetail.find\_by\_bedroom\_id(:all)  
else  
@listings=ListingDetail.find\_all\_by\_bedroom\_id(bedroom\_id)  
end  
@lists \<\<@listings  
garage\_id=params[:garage][:id]  
if garage\_id==1  
@listings=ListingDetail.find\_by\_garage\_id(:all)  
else  
@listings=ListingDetail.find\_all\_by\_bedroom\_id(garage\_id)  
end  
@lists \<\<@listings  
@lists =@lists.uniq! #as tye array will definitely contain duplicate  
elements  
end  
above way is not a better soln it will be better if you use query.

---

<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, 7:44am UTC](https://www.ruby-forum.com/t/search-conditons/119914/8 "2007-11-12T07:44:35Z")

</div>

I think this is because as your last piece of code is this

if garage\_id==1  
@listings=ListingDetail.find\_by\_garage\_id(:all)  
else  
@listings=ListingDetail.find\_all\_by\_bedroom\_id(garage\_id)  
end

the value of @listings will be ListingDetail.find\_by\_garage\_id(:all) or  
ListingDetail.find\_all\_by\_bedroom\_id(garage\_id) as you haven’t appended  
the earlier values you are actually overwriting the value of listings

Try using query for all these requirements then  
ListingDetail.find\_by\_garage\_id(:all,:conditions[query])
