【问题标题】:ActiveAdmin filter: a has_many: through attributeActiveAdmin 过滤器:一个 has_many:通过属性
【发布时间】:2020-02-20 18:34:16
【问题描述】:

我想过滤通过 has_many :foo 到: :bar 关系链接的模型属性上的 ActiveAdmin 表。

例如,如果一本书

has_many :borrowers, through: :book_checkouts

...如何在图书的 ActiveAdmin 页面上添加过滤器选项,让用户过滤borrower_id? (作为一个字符串;我们的图书馆有太多的借阅者,所以在这里使用 Select 很不方便。)

【问题讨论】:

    标签: ruby-on-rails many-to-many activeadmin


    【解决方案1】:

    背景

    在您的 ActiveAdmin 页面中,您可以像这样添加过滤器:

    filter :column_name_1
    filter :column_name_2
    

    因此,您将为borrowers 创建一个新的过滤器语句,但默认情况下,它将创建一个选择。

    filter :borrowers
    

    解决方案

    因此,为了告诉它您不想使用选择,您必须使用要过滤的关联中的哪一列来修改您给它的符号。因此,如果您想按借款人的 ID 进行过滤,您可以这样做:

    # You specify as: :numeric to so ActiveAdmin knows to add 
    # filtering options for numbers. So when it renders on the page,
    # for the filter option it will display three options to filter by:
    # ID equals, is greater than, or is less than
    
    filter :borrowers_id, as: :numeric
    

    如果您想按另一个字符串列进行过滤,例如 name,您将执行以下操作:

    filter :borrowers_name, as: :string
    

    注意:我还注意到如果您没有指定 as: :type,那么 ActiveAdmin 将无法识别过滤器并且不会为它呈现任何内容。

    得到问题答案的帮助:Active Admin - filter by presence of has_many association

    【讨论】:

      猜你喜欢
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多