title: Searching on two conditions in MongoDB in Ruby on Rails
body: <p>I am using rails cast video <a href="http://railscasts.com/episodes/240-search-sort-paginate-with-ajax" rel="nofollow">http://railscasts.com/episodes/240-search-sort-paginate-with-ajax</a> . 
  I have to use two conditions to search in my database.</p>
  
  <pre><code># In my form for search 
  &lt;%= form_tag products_path, :method =&gt; 'get', :id =&gt; "products_search" do %&gt;
    &lt;p&gt;
      &lt;%= text_field_tag :search, params[:search] %&gt;
      &lt;%= submit_tag "Search", :name =&gt; nil%&gt;
    &lt;/p&gt;
  </code></pre>
  
  <p>For one condition, this works fine:</p>
  
  <pre><code># In my model product.rb
  def self.search(search)
    if search
      where(name: /#{Regexp.escape(search)}/i) 
    else
      scoped
    end
  end
  
  # In my controller products_controller.rb
  @products = Product.search(params[:search]).page(params[:page]).per(5)
  </code></pre>
  
  <p>Now I have to search with another parameter, <code>role</code>. I have role field in my product Model. role is 1,2,3. If role is given then it should search string with given input role, else only search with name.</p>
  
  <pre><code>def self.search(search,role)
              if search
  where(name: /#{Regexp.escape(search)}/i)(and role: should be equal to value of role)
  </code></pre>
  
  <p>Also, what changes should I do in my products_controller.rb? And in my above search form?</p>