How to search through a polymorphic relationship

45 views Asked by At

I have the following model:

class Measurement < ApplicationRecord
    belongs_to :service
end

And this other one:

class Service < ApplicationRecord
    belongs_to :serviceable, polymorphic: true
    has_many :measurements
end

And these other two, which are the models that can take the place of the serviceable:

class Unit < ApplicationRecord
    belongs_to :enterprise
    has_many :services, as: :serviceable
end

class CommonArea < ApplicationRecord
    belongs_to :enterprise
    has_many :services, as: :serviceable
end

I have a search form and I want to only search for measurements that are from a specific enterprise, so I tried to do the search as follows:

 <div class="input-field">
      <%= f.label :services_serviceable_enterprise_id_in, "Enterprises" %>
      <%= f.grouped_collection_select(
           :services_serviceable_enterprise_id_in, 
           Town.active, 
           lambda { |town| town.enterprises.releaseds }, 
           :name, :id, :name, { include_blank: "" }
      ) %> 
 </div>

But I get the following error: "Polymorphic association does not support to compute class". How to resolve this?

0

There are 0 answers