简体   繁体   中英

Filterrific gem for two tables

I am using filterrific gem to add filters in my app. I have parents table and children table. On the children_list page which displays list of all the children with their firstname and their parent's firstname. The issue I am facing is in the search query I want to add the parent.firstname search as well for filterrific. I tried adding a join as below:-

num_or_conds = 2
 joins(child: :parent).where(
   terms.map { |term|
    "(LOWER(children.firstname) LIKE ?) OR (LOWER(parents.firstname) LIKE ?) "

But this didnt do the job. Any idea how this can be achieved.

parent.rb

    has_many :children

child.rb

belongs_to :parent

filterrific(
    available_filters: [
      :search_query,
      :with_clinic_date
    ]
  )

  scope :search_query, lambda { |query|
    return nil  if query.blank?

    terms = query.downcase.split(/\s+/)

    terms = terms.map { |e|
      (e.gsub('*', '%') + '%').gsub(/%+/, '%')
    }

    num_or_conds = 2
    where(
      terms.map { |term|
        "(LOWER(children.firstname) LIKE ?) OR (LOWER(parents.firstname) LIKE ?)"
      }.join(' AND '),
      *terms.map { |e| [e] * num_or_conds }.flatten
    )
  }

  scope :with_clinic_date, lambda { |ref_date|
    where('children.clinic_date = ?', ref_date)
  }

end

_children.html.erb

<h1>Children</h1>
  <div class="table-responsive">
    <table class="table table-bordered">
      <thead>
        <tr>
          <th>Parent First Name</th> 
          <th>Child firstname</th>

        </tr>
      </thead>

      <tbody>
        <% @children.each do |child| %>
          <tr>
            <td><%=child.parent.firstname %></td>
            <td><%=child.firstname %></td>
          </tr>
        <% end %>
      </tbody>
    </table>
  </div> 
</div>

children-list.html.erb

<%= form_for_filterrific @filterrific do |f| %>
  <div class="row">
    <div class="col-sm-3">
      Search
      <%= f.text_field(
        :search_query,
        class: 'filterrific-periodically-observed form-control'
      ) %>
    </div>
    <div class="col-sm-3">
      Request Date
      <%= f.date_field(:with_clinic_date, class: 'js-datepicker form-control') %>
    </div>

    <div class="col-sm-3">
    <br>
    <%= link_to(
      'Reset filters',
      reset_filterrific_url,
    ) %>
  </div>
</div>
  <%= render_filterrific_spinner %>
<% end %>
<%= render( partial: 'children/children_list') %>

children.js.erb

<% js = escape_javascript(
  render(partial: 'children/children_list')
) %>
$("#filterrific_results").html("<%= js %>");

AFAIK, you can't filter two separate classes on the same page. It will use the last defined filterrific instance. When I ran into this problem, I used remote forms with custom action/routes

# routes.rb
resources :parent do
  get :filter_parents
  resources: children do
    get :filter_children
  end
end

And then the controllers..

# parents_controller.rb
def index
  parents_filter # this would be a helper method running your filter queries
end

def filter_parents
  parents_filter # this would be a helper method running your filter queries
end

The children's controller would look similar, just different named helper method/custom action.

And then use a partial for the table. Target the table's container, and use a filter_parents.js.erb and filter_childrens.js.erb file

$('#parents-table').html('<%= escape_javascript render 'path/to/partial'%>')
// same in childrens.js.erb, just target the appropriate table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM