Search code examples
ruby-on-railsrubysearchfilterransack

Multiple polymorphic association search using Ransack, is it possible?


I have the class Invoice that belongs_to Invoiceable, a polymorphic class. The Invoiceable of Invoice can either be Subscription or Purchase.

I need to be able to search by the status of an Invoice's Subscription or Purchase.

I'm trying the following to no success:

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

I tried different combinations like:

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

But I always get a Polymorphic associations do not support computing the class. error.

Is this possible? Ransack documentation only explains how to search by a single polymorphic associated model.


Solution

  • You were very close to the answer.

    You need to specify column name with each of the polymorphic association

    use below combination :

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

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