I have a search functionality in my app which works as the following
- Homepage: user selects location from a drop-down and then enters a search key-word and searches to get a set of results
- Search Page (REfine Search Options): Once the user hits this page from the above he will be provided with more options to refine the search and narrow the results.
Right now we are implementing as follows but i am assuming as the paramenters increase over 5-7 the number of combinations will increase the number of if-else-elseif statement as well.
#refine search
@search = params[:search]
if params[:city].present? && params[:location_ids].present? && params[:search].blank?
@blood_banks = BloodBank.where(
{ :city_id => "#{@city}" }).where(
{ :location_id.in => params[:location_ids] })
elsif params[:search].present? && params[:location_ids].blank?
@blood_banks = BloodBank.where(
{ :bb_name => /#@search/i })
elsif params[:search].present? && params[:city].present? && params[:location_ids].present?
@blood_banks = BloodBank.where(
{ :city_id => "#{@city}" }).where(
{ :location_id.in => params[:location_ids] }).where(
{ :bb_name => /#@search/i })
end
Which is the best way to implement the same.
How do you achieve below code,
if params[:gender].present?
if params[:gender] == "male"
@doctors = Doctor.where( :gender => "Male")
end
if params[:gender] == "female"
@doctors = Doctor.where( :gender => "Female")
end
if params[:gender] == "any"
@doctors = Doctor.where( :gender => "Male") || Doctor.where( :gender => "Female")
end
end