【问题标题】:Adding filter for has_many relationship in rails_admin在 rails_admin 中为 has_many 关系添加过滤器
【发布时间】:2016-01-06 00:49:30
【问题描述】:

我也使用 rails_admin 作为报告工具,并利用导出到 CSV 功能来提取特定数据。这很好用,但我现在需要根据两个日期之间的数据过滤结果,但日期在通过 has_many 关系链接的模型上。

这是一个例子

class Patient < ActiveRecord::Base
  has_many :appointments
end

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

我可以在 RailsAdmin 中加载患者并在列表视图中显示约会,但是如何添加过滤器以仅显示在日期 X 和 Y 之间有约会的患者?这可能吗?

有没有使用命名作用域并传入参数?或者,要加载 rails_admin 的虚拟模型呢? SQL 对此非常简单,但我只是不确定如何传入实际的日期参数?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 rails-admin


    【解决方案1】:

    我也很好奇如何解决这个问题。

    一种选择是将约会添加到过滤下拉列表中,但它不允许您选择日期。

    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
    

    另一种选择是创建custom action 并在 app/views/rails_admin/mail/filter.html.haml 创建页面,您可以在其中构建常规搜索表单

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      • 2012-08-29
      相关资源
      最近更新 更多