I am using rails cast video http://railscasts.com/episodes/240-search-sort-paginate-with-ajax . I have to use two conditions to search in my database.
# In my form for search
<%= form_tag products_path, :method => 'get', :id => "products_search" do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil%>
</p>
For one condition, this works fine:
# 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)
Now I have to search with another parameter, role
. 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.
def self.search(search,role)
if search
where(name: /#{Regexp.escape(search)}/i)(and role: should be equal to value of role)
Also, what changes should I do in my products_controller.rb? And in my above search form?