简体   繁体   English

在rails_admin中为has_many关系添加过滤器

[英]Adding filter for has_many relationship in rails_admin

I'm using rails_admin as a reporting tool as well and leveraging the export to CSV feature to pull out specific data. 我也使用rails_admin作为报告工具,并利用导出到CSV功能提取特定数据。 This works fine, but I need to now filter results based on data being between 2 dates, but the dates are on on a model that is linked through a has_many relationship. 这可以正常工作,但是我现在需要根据两个日期之间的数据过滤结果,但是这些日期位于通过has_many关系链接的模型上。

Here is an example 这是一个例子

class Patient < ActiveRecord::Base
  has_many :appointments
end

class Appointment < ActiveRecord::Base
  belongs_to :patient
  # has field: 'date'
end

I can load patients in RailsAdmin and in the list view show the appointments, but how can I add a filter to only show patients that have an appointment between Date X and Y? 我可以在RailsAdmin中加载患者,并在列表视图中显示约会,但是如何添加过滤器以仅显示在日期X和Y之间进行约会的患者? Is this possible at all? 这有可能吗?

Is there anyway to use named scope and pass in parameters? 无论如何,有没有使用命名范围和传入参数的方法? Or what about a virtual model for rails_admin to load? 还是要加载rails_admin的虚拟模型? The SQL is pretty simple for this, but I'm just not sure how to pass in the actual date parameters? SQL非常简单,但是我不确定如何传递实际的日期参数?

I am also curious as to how to solve this. 我也很好奇如何解决这个问题。

One option is to add appointments to filter dropdown but it will not allow you to choose dates. 一种选择是添加约会以过滤下拉菜单,但不允许您选择日期。

rails_admin do
  include_all_fields
  field :appointments do
    filterable true
  end
  list do
    filters [:appointments] # this will make it show up by default
  end
end

Another choice is to create custom action and create page at app/views/rails_admin/mail/filter.html.haml where you can build regular search form 另一种选择是在app / views / rails_admin / mail / filter.html.haml中创建自定义操作并创建页面,您可以在其中构建常规搜索表单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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