jhund / filterrific

Filterrific is a Rails Engine plugin that makes it easy to filter, search, and sort your ActiveRecord lists.
http://filterrific.clearcove.ca
MIT License
910 stars 125 forks source link

Wrong number of arguments (given 1, expected 0) in find method #149

Closed Leopard2A5 closed 6 years ago

Leopard2A5 commented 6 years ago

Hi! Hope you can help me!

rails 5.1.4 filterrific 2.1.2 will_paginate 3.1.6

model:

class Student < ApplicationRecord
  filterrific(
    default_filter_params: { sort_by: 'lastname_asc' },
    available_filters: [
      :sort_by,
      :with_lastname
    ]
  )
end

controller:

def index
    @filterrific = initialize_filterrific(
      Student,
      params[:filterrific]
    ) or return
    @students = @filterrific.find.page(params[:page])

    # @students = Student.where(deleted: false)
    respond_to do |format|
      format.html
      format.js
    end
  end

index.html.erb:

<%= form_for_filterrific @filterrific do |f| %>
  <div>
    Sorted by
  </div>
  <div>
    <%= link_to('Reset filters', reset_filterrific_url) %>
  </div>
  <%= render_filterrific_spinner %>
<% end %>

<%= render(partial: 'students/list', locals: { students: @students }) %>

_list.html.erb:

<div id="filterrific_results">
  <div>
    <%= page_entries_info students %>
  </div>

  <table class="table">
    <thead>
      <tr>
        <th><%= t :firstname %></th>
        <th><%= t :lastname %></th>
        <th><%= t :mobile %></th>
        <th><%= t :phone %></th>
        <th><%= t :email %></th>
        <th><%= t :birthdate %></th>
        <th colspan="3"></th>
      </tr>
    </thead>

    <tbody>
      <% students.each do |student| %>
        <tr>
          <td><%= student.firstname %></td>
          <td><%= student.lastname %></td>
          <td><%= student.mobile %></td>
          <td><%= student.phone %></td>
          <td><%= student.email %></td>
          <td><%= l student.birthdate %></td>
          <td><%= link_to t(:show), student %></td>
          <td><%= link_to t(:edit), edit_student_path(student) %></td>
          <td><%= link_to t(:destroy), student, method: :delete, data: { confirm: t(:are_you_sure) } %></td>
        </tr>
      <% end %>
    </tbody>
  </table>

</div>

<%= will_paginate students %>

When I navigate to /students now, i instantly get this error: wrong number of arguments (given 1, expected 0) with the following stacktrace:

filterrific (2.1.2) lib/filterrific/active_record_extension.rb:69:in `sort_by'
filterrific (2.1.2) lib/filterrific/active_record_extension.rb:69:in `block in filterrific_find'
filterrific (2.1.2) lib/filterrific/active_record_extension.rb:66:in `each'
filterrific (2.1.2) lib/filterrific/active_record_extension.rb:66:in `filterrific_find'
filterrific (2.1.2) lib/filterrific/param_set.rb:45:in `find'
app/controllers/students_controller.rb:11:in `index'

I've followed the stacktrace this far: In filterrific (2.1.2) lib/filterrific/active_record_extension.rb:69:inblock in filterrific_find'` the following line fails:

ar_rel = ar_rel.send(filter_name, filter_param)

filter_name has the value "sort_by" and filter_param is "lastname_asc", but ar_rel#sort_by has an arity of 0.

Is this a bug or am I doing something wrong?

jhund commented 6 years ago

@Leopard2A5 you need to specify a scope for every filter_name, in your case, I don't see a scope named sort_by in your Student model.