两个表的Filterrific gem

问题描述 投票:0回答:1

我正在使用filterrific gem在我的应用中添加过滤器。我有父母桌和儿童桌。在children_list页面上,该页面显示所有具有其名字的子项及其父项名字的列表。我面临的问题是在搜索查询中我想添加parent.firstname搜索以及filterrific。我尝试添加如下连接: -

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

但这并没有完成这项工作。知道如何实现这一目标。

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>

孩子,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 %>");
ruby-on-rails rubygems ruby-on-rails-5 filterrific
1个回答
0
投票

AFAIK,您无法在同一页面上过滤两个单独的类。它将使用最后定义的filterrific实例。当我遇到这个问题时,我使用了带有自定义操作/路由的远程表单

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

然后控制器..

# 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

孩子们的控制器看起来很相似,只是不同的命名辅助方法/自定义动作。

然后使用部分表格。定位表的容器,并使用filter_parents.js.erbfilter_childrens.js.erb文件

$('#parents-table').html('<%= escape_javascript render 'path/to/partial'%>')
// same in childrens.js.erb, just target the appropriate table
© www.soinside.com 2019 - 2024. All rights reserved.