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

class Location
  belongs_to :global_company
  has_one :global_company_form

class Company
  belongs_to :global_company
  belongs_to :subdomain

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

class GlobalCompanyForm
  belongs_to :location
  belongs_to :subdomain_form

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

class SubdomainForm
  belongs_to :subdomain


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.


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:



I developed this based on the discussion here .



All Articles