title: Rails/MongoDb Search and Refine Search Implementation
body: <p>I have a search functionality in my app which works as the following</p>
  
  <ol>
  <li>Homepage: user selects location from a drop-down and then enters a search key-word and searches to get a set of results</li>
  <li>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. </li>
  </ol>
  
  <p>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.</p>
  
  <pre><code>#refine search
      @search = params[:search]
  
  
      if params[:city].present? &amp;&amp; params[:location_ids].present? &amp;&amp; params[:search].blank?
        @blood_banks = BloodBank.where(
          { :city_id =&gt; "#{@city}" }).where(
          { :location_id.in =&gt; params[:location_ids] })
      elsif params[:search].present? &amp;&amp; params[:location_ids].blank?
          @blood_banks = BloodBank.where(
            { :bb_name =&gt; /#@search/i })
      elsif params[:search].present? &amp;&amp; params[:city].present? &amp;&amp; params[:location_ids].present?
          @blood_banks = BloodBank.where(
            { :city_id =&gt; "#{@city}" }).where(
            { :location_id.in =&gt; params[:location_ids] }).where(
            { :bb_name =&gt; /#@search/i })
      end 
  </code></pre>
  
  <p>Which is the best way to implement the same.</p>
  
  <p>How do you achieve below code,</p>
  
  <pre><code>if params[:gender].present?
        if params[:gender] == "male"
        @doctors = Doctor.where( :gender =&gt; "Male")
        end
        if params[:gender] == "female"
        @doctors = Doctor.where( :gender =&gt; "Female")
        end
        if params[:gender] == "any"
        @doctors = Doctor.where( :gender =&gt; "Male") || Doctor.where( :gender =&gt; "Female")
        end
  end
  </code></pre>