2016-12-15 42 views
0

搜索的has_many我在Rails的新手,有問題與洗劫:洗劫:通過關聯

這是樣板工程

class Project < ApplicationRecord 
    searchkick 
    belongs_to :company 
    belongs_to :m_category 
    has_many :project_industries, dependent: :destroy 
    has_many :m_industries, through: :project_industries 

end 

這是模型行業:

class Industry < ApplicationRecord 
    include M 
    belongs_to :m_industry_category 
    has_many :project_industries, dependent: :destroy, foreign_key: :industry_id 
    has_many :projects, through: :project_industries 
end 

這是模型IndustryCategory:

class IndustryCategory < ApplicationRecord 
    has_many :industries, dependent: :destroy, 
    foreign_key: :industry_category_id 

    has_many :projects, through: :industries 
end 

現在,我想通過IndustryCategory搜索該項目,但我不知道如何。請幫幫我!! TKS

回答

0

您可以使用類似這樣

@industrty_category = IndustryCategory.find(params[:id]) 

@project = @industry_category.projects.all 
+0

我認爲它不會被洗劫:( –

+0

搜索我沒有得到你想說你能解釋一下什麼正確。 –

+0

我給你的AR查詢所以你可以相應地使用Ransack。 –