【问题标题】:Multiple polymorphic association search using Ransack, is it possible?使用 Ransack 进行多态关联搜索,可以吗?
【发布时间】:2021-11-01 18:14:25
【问题描述】:

我有 Invoicebelongs_to Invoiceable,一个多态类。 Invoice 的Invoiceable 可以是SubscriptionPurchase

我需要能够按发票的SubscriptionPurchase 的状态进行搜索。

我正在尝试以下方法,但没有成功:

<%= f.check_box :invoiceable_of_Subscription_or_Purchase_type_status_in, { multiple: true }, type[1], false %>

我尝试了不同的组合,例如:

<%= f.check_box :invoiceable_of_Subscription_type_or_invoiceable_of_Purchase_type_status_in, { multiple: true }, type[1], false %>

但我总是收到Polymorphic associations do not support computing the class. 错误。

这可能吗? Ransack 文档仅说明了如何通过单个多态关联模型进行搜索。

【问题讨论】:

    标签: ruby-on-rails ruby search filter ransack


    【解决方案1】:

    你已经非常接近答案了。

    您需要为每个多态关联指定列名

    使用以下组合:

    <%= f.check_box :invoiceable_of_Subscription_type_status_or_invoiceable_of_Purchase_type_status_in, { multiple: true }, type[1], false %>
    

    参考:https://github.com/activerecord-hackery/ransack/wiki/Polymorphic-searches

    【讨论】:

      最近更新 更多