[英]Ransack: has_many through association expecting param throws error
這是我的模型的一般近似,通過使用需要參數的范圍來使用has_many
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將拋出:
NoMethodError: undefined method `subdomain' for #<ActiveRecord::Associations::JoinDependency::JoinAssociation:0x007fbd227f0850>
當ransack訪問該關聯時,它將其作為“公司”傳遞給關聯,而不是公司記錄,因此是no方法
我環顧四周,但沒有找到任何類似的例子,我無法弄清楚如何進行搜索,尊重這種類型的關聯/范圍。 該協會本身在洗劫之外工作正常。
您只需在控制器中手動執行連接,同時指定要從以下位置繪制結果的表:
@q= GlobalCompany.joins(:location, :global_company_forum).ransack(params[:q])
在設置表單時,使用:
<%=f.search_field :global_company_forum_name_contains%>
為了獲得結果,只需將模型鏈接在一起:
global_company_instance.location.global_company_forum.name
我根據這里的討論開發了這個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.