Search conditons

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

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

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

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

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

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

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 [email protected]! #as tye array will definitely contain duplicate
elements
end
above way is not a better soln it will be better if you use query.

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])