Here's a generic approximation of my models, with a has_many through using a scope that expects a parameter
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 throw:
NoMethodError: undefined method `subdomain' for #<ActiveRecord::Associations::JoinDependency::JoinAssociation:0x007fbd227f0850>
when ransack accesses that association its passing in an association as the 'company', instead of a company record, hence the no method
I've looked around, but haven't found any similar examples and I cannot figure out how to make ransack respect this type of association/scope. The association itself works fine outside of ransack.
You just have to perform the join manually in your controller when specifying the table to draw results from:
@q= GlobalCompany.joins(:location, :global_company_forum).ransack(params[:q])
And when set up the form, use:
<%=f.search_field :global_company_forum_name_contains%>
And for results, just chain the models together:
global_company_instance.location.global_company_forum.name
I developed this based on a discussion here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.