Multiple search filter like the one in freshdesk I have requirement to apply multiple filters on database. I will have multiple conditions in the front end where the user can select the conditions he wants to filter with and then see the output. This is similar to the filter that Freshdesk has or for instance similar to many ecommerece sites.

This is my CoffeeScript file:

read = -&gt;
$("#payment_status_filter :checkbox").click -&gt;
    collect_data()
$("#status_filter :checkbox").click -&gt;
    collect_data()

collect_data = -&gt;
    status_array = []
    if $("#status_filter :checkbox").is(":checked")
        console.log("####*******status filter##########@@@@@@")
        $("#status_filter :checked").each -&gt;
            status_array.push $(this).val()
        console.log(status_array)
    payment_status_array = []
    if $("#payment_status_filter :checkbox").is(":checked")
        $("#payment_status_filter :checked").each -&gt;
            console.log("*****PAYMENT STATUS**********")
            payment_status_array.push $(this).val()
            console.log(payment_status_array)
    ajax_call(payment_status_array,status_array)

ajax_call= (payment_status,status) -&gt;
    $.ajax(
        type: "GET"
        url: "/admin/filter_request"
        format: 'js'
        dataType: "script"
        data:
            payment_status: payment_status
            status: status
    ).done (data) -&gt;
        console.log data
        $(".request_table_span").html data
        return

$(document).ready read
$(document).on "page:load", read


What I am doing above is that when the user clicks a checkbox for a payment_status conditions say "Paid" or "Unpaid" then it will fire a ajax request to a controller which will respond with a result. I have written such that when the requisite condition is clicked all the conditions in the whole page will be collected and sent along because more than one filter can be applied at the same time.

The following is my controller code:

def filter_request
    Rails.logger.info params
    @requests = Request.all
    @requests = @requests.where(payment_status: params[:payment_status]) if params[:payment_status]
    @requests = @requests.where(status: params[:status]) if params[:status]
    respond_to do |format|
       format.js
    end
end


The above approach seems not very good when the requirement has many filters, especially the controller part. How do I go about this? Any better approaches?
I would advise you to implement search in Index action. If there are no search filters passed, then simply return everything (both for HTML and JSON).

You should rebuild a bit of the form so search params are nested, and you won't have to filter them.

Finally, you can move the search logic to model:

class Request
  def self.search(opts={})
    #reject empty filters
    filters = opts.reject(&amp;:blank?)
    where(filters)
  end
end

class RequestsController
  def index
    @requests = Request.search(params[:search])
  end
end


Now you need to worry only about passing correct params (form fields should match db columns). Some transformations may be necessary depending on how you expect to build the query.

Be sure to fully test the form with integration spec; you are relying on view a lot.