title: Filterrific on Rails 5
body: <p>Does anyone have experience with Rails 5 and Filterrific? </p>
  
  <p>I was following the documentation - <a href="http://filterrific.clearcove.ca/" rel="nofollow">http://filterrific.clearcove.ca/</a> - did not work, although I did not receive any errors either. It does not update the results and debugging it shows that it reads all entries without applying filter values. </p>
  
  <p>I was also downloading the demo application to adopt it - <a href="https://github.com/jhund/filterrific_demo" rel="nofollow">https://github.com/jhund/filterrific_demo</a> - Did not work either - same results.</p>
  
  <p>Rails version: Rails 5.0.0
  Version filterrific: filterrific (2.0.5)
  Ruby version: ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-darwin15]</p>
  
  <p>Tables</p>
  
  <p>contacts: (belongs_to contact_type)</p>
  
  <p>title
  content
  content_type_id</p>
  
  <p>contact_types: (has_many contacts)</p>
  
  <p>name</p>
  
  <p><strong>Contact Model</strong></p>
  
  <pre><code>class Contact &lt; ApplicationRecord
  
    filterrific(
        default_filter_params: { :sorted_by =&gt; 'created_at_desc' },
        :available_filters =&gt; %w[
            sorted_by
            search_query
            with_contact_type_id
            with_created_at_gte
        ]
    )
  
    self.per_page = 10
  
    belongs_to :contact_type
    belongs_to :user
  
    validates :contact_type_id, presence: true
    validates :content, presence: true
  
    scope :search_query, lambda { |query|
      return nil  if query.blank?
  
      terms = query.downcase.split(/\s+/)
  
      terms = terms.map { |e|
        (e.gsub('*', '%') + '%').gsub(/%+/, '%')
      }
      num_or_conditions = 2
      where(
          terms.map {
            or_clauses = [
                "LOWER(contacts.title) LIKE ?",
                "LOWER(contacts.content) LIKE ?"
            ].join(' OR ')
            "(#{ or_clauses })"
          }.join(' AND '),
          *terms.map { |e| [e] * num_or_conditions }.flatten
      )
    }
    scope :sorted_by, lambda { |sort_option|
      direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
  
      case sort_option.to_s
        when /^created_at_/
          order("contacts.created_at #{ direction }")
        when /^contact_type_name_/
          order("LOWER(contact_types.name) #{ direction }").includes(:contact_type)
        else
          raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
      end
    }
    scope :with_contact_type_id, lambda { |contact_type_ids|
      where(:contact_type_id =&gt; [*contact_type_ids])
    }
    scope :with_created_at_gte, lambda { |ref_date|
      where('contacts.created_at &gt;= ?', ref_date)
    }
  
    delegate :name, :to =&gt; :contact_type, :prefix =&gt; true
  
    def self.options_for_sorted_by
      [
          ['Date received (newest first)', 'created_at_desc'],
          ['Date received (oldest first)', 'created_at_asc'],
          ['Subject (a-z)', 'contact_type_name_asc']
      ]
    end
  
  end
  </code></pre>
  
  <p><strong>ContactType Model</strong></p>
  
  <pre><code>class ContactType &lt; ApplicationRecord
    translates :name
  
    has_many :contacts, :dependent =&gt; :nullify
  
    def self.options_for_select
      order('LOWER(name)').map { |e| [e.name, e.id] }
    end
  end
  </code></pre>
  
  <p><strong>Contact Controller - Index</strong></p>
  
  <pre><code>class ContactsController &lt; ApplicationController
    before_action :set_contact, only: [:show, :edit, :update, :destroy]
    layout 'sidenav'
    include Pundit
    # helper_method :sort_column, :sort_direction
  
    # GET /contacts
    # GET /contacts.json
    def index
      @filterrific = initialize_filterrific(
          Contact,
          params[:filterrific],
          select_options: {
              sorted_by: Contact.options_for_sorted_by,
              with_contact_type_id: ContactType.options_for_select
          },
          persistence_id: 'shared_key',
          default_filter_params: {},
          available_filters: [],
      ) or return
  
      @contacts = @filterrific.find.paginate(page: params[:page], per_page: 25)
  
      authorize @contacts
  
      # Respond to html for initial page load and to js for AJAX filter updates.
      respond_to do |format|
        format.html
        format.js
      end
  
    rescue ActiveRecord::RecordNotFound =&gt; e
      # There is an issue with the persisted param_set. Reset it.
      puts "Had to reset filterrific params: #{ e.message }"
      redirect_to(reset_filterrific_url(format: :html)) and return
    end
  
    ...
  
  end
  </code></pre>
  
  <p><strong>Views</strong></p>
  
  <p>index.html.haml</p>
  
  <pre><code>      = form_for_filterrific @filterrific do |f|
          %div{:style =&gt; "width: 25%;float: left;"}
            Search
            = f.text_field(:search_query,
                class: 'filterrific-periodically-observed form-control')
          %div{:style =&gt; "width: 25%;float: left;"}
            Subject
            #{f.select( :with_contact_type_id, @filterrific.select_options[:with_contact_type_id], { include_blank: '- Any -' }, :class =&gt; "form-control" )}
          %div{:style =&gt; "width: 25%;float: left;"}
            Sent after
            #{f.text_field(:with_created_at_gte, :"data-provide" =&gt; 'datepicker', :class =&gt; "form-control")}
          %div{:style =&gt; "width: 25%;float: left;"}
            Sorted by
            #{f.select(:sorted_by, @filterrific.select_options[:sorted_by], {}, :class =&gt; "form-control")}
          %div
            = link_to('Reset filters', reset_filterrific_url)
          = render_filterrific_spinner
        #results
          = render(partial: 'contacts/list_contacts', locals: { contacts: @contacts })
  </code></pre>
  
  <p>_list_contacts.html.haml</p>
  
  <pre><code>%table.table
    %thead
      %tr
        %th= "Subject"
        /= sortable "contact_type_id", "Subject", params[:filter]
        %th Message
        %th= "Sender"
        /= sortable "user_id", "Sender", params[:filter]
        %th= "Status"
        %th= "Created"
        /= sortable "created_at", "Created", params[:filter]
        %th
    %tbody
      - contacts.each do |contact|
        %tr
        %td
          - unless contact.contact_type.nil?
            = contact.contact_type.name
        %td= truncate(contact.content, length: 100)
        %td= contact.user.email
        %td= check_status(contact.closed)
        %td= contact.created_at.strftime("%B %d, %Y")
        %td.actions
          = link_to fa_icon_tag("eye-open"), contact, rel: 'tooltip', title: 'Show'
          = link_to fa_icon_tag("pencil"), edit_contact_path(contact), rel: 'tooltip', title: 'Edit'
          = link_to fa_icon_tag("trash"), contact, rel: 'tooltip', title: 'Delete', data: { confirm: 'Are you sure?' }, method: :delete
  </code></pre>
  
  <p>index.js.erb</p>
  
  <pre><code>&lt;% js = escape_javascript(
    render(
      :partial =&gt; 'contacts/list_contacts',
      :locals =&gt; { contacts: @contacts }
    )
  ) %&gt;
  $("#results").html("&lt;%= js %&gt;");
  </code></pre>
  
  <p><strong>Console output</strong></p>
  
  <pre><code>Started GET "/en/dashboard/admin/support/contacts?utf8=%E2%9C%93&amp;filterrific%5Bsearch_query%5D=Hello&amp;filterrific%5Bwith_contact_type_id%5D=1&amp;filterrific%5Bwith_created_at_gte%5D=&amp;filterrific%5Bsorted_by%5D=created_at_desc&amp;_=1475267744268" for ::1 at 2016-09-30 21:35:51 +0100
  DEBUG: Chewy strategies stack: [2] &lt;- atomic @ /Users/georg/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/chewy-0.8.4/lib/chewy/railtie.rb:17
  Processing by ContactsController#index as JS
    Parameters: {"utf8"=&gt;"✓", "filterrific"=&gt;{"search_query"=&gt;"Hello", "with_contact_type_id"=&gt;"1", "with_created_at_gte"=&gt;"", "sorted_by"=&gt;"created_at_desc"}, "_"=&gt;"1475267744268", "locale"=&gt;"en"}
    User Load (0.8ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 4], ["LIMIT", 1]]
    ContactType Load (0.6ms)  SELECT "contact_types".* FROM "contact_types" ORDER BY LOWER(name)
    Rendering contacts/index.js.erb
    Contact Load (0.6ms)  SELECT  "contacts".* FROM "contacts" ORDER BY contacts.created_at desc LIMIT $1 OFFSET $2  [["LIMIT", 25], ["OFFSET", 0]]
    ContactType Load (0.6ms)  SELECT  "contact_types".* FROM "contact_types" WHERE "contact_types"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
    User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 9], ["LIMIT", 1]]
    CACHE (0.0ms)  SELECT  "contact_types".* FROM "contact_types" WHERE "contact_types"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
    CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 9], ["LIMIT", 1]]
    CACHE (0.0ms)  SELECT  "contact_types".* FROM "contact_types" WHERE "contact_types"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
    User Load (0.8ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 8], ["LIMIT", 1]]
    Rendered contacts/_list_contacts.html.haml (27.0ms)
    Rendered contacts/index.js.erb (31.7ms)
  Completed 200 OK in 150ms (Views: 101.3ms | ActiveRecord: 4.2ms)
  </code></pre>