Ransack: has_many via association awaiting paramsrows error

Here's a general approximation of my models with has_many using the scope that the parameter expects

class GlobalCompany
  has_many :locations
  has_many :global_company_forms, :through => :locations
end

class Location
  belongs_to :global_company
  has_one :global_company_form
end

class Company
  belongs_to :global_company
  belongs_to :subdomain

  has_many :global_company_forms, ->(company) { for_company(company) }, :through => :global_company
end

class GlobalCompanyForm
  belongs_to :location
  belongs_to :subdomain_form

  scope :for_company, ->(company) {where(:subdomain_form_id => company.subdomain.subdomain_form.id)}
end

class SubdomainForm
  belongs_to :subdomain
end

      

Company.ransack (q) .result will call:

NoMethodError: undefined method `subdomain' for #<ActiveRecord::Associations::JoinDependency::JoinAssociation:0x007fbd227f0850>

      

when ransack accesses this association passing in the association as "company" instead of the company record, hence method no

I've looked around but haven't found any similar examples and I can't figure out how to make the ransack respect this type of association / scope. The association itself works great outside of the search.

+3


source to share


1 answer


You just need to do the connection manually in your controller, specifying a table to get the results:

@q= GlobalCompany.joins(:location, :global_company_forum).ransack(params[:q])

      

And when setting up your form use:

<%=f.search_field :global_company_forum_name_contains%>

      



And to get the results, just wire the models together:

global_company_instance.location.global_company_forum.name

      

I developed this based on the discussion here .

+2


source







All Articles