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
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
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
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])
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.