2015-11-23 127 views
1

我有一個雙重的has_many:通過關係是這樣的:查詢雙的has_many:通過關聯

class Survey < ActiveRecord::Base 
    has_many :surveyquestions 
    has_many :questions, through: :surveyquestions 
end 

class Question < ActiveRecord::Base 
    has_many :surveyquestions 
    has_many :surveys, through: :surveyquestions 
    has_many :kpiquestions, dependent: :destroy 
    has_many :kpis, through: :kpiquestions 
end 

class Kpi < ActiveRecord::Base 
    has_many :kpiquestions 
    has_many :questions, through: :kpiquestions 
end 

現在我想查詢具有特定survey_id和特定kpi_id問題,但我不知道該如何查詢這個?

回答

1

嘿,你可以嘗試這樣

Question.includes(:surveys,:kpis).where(:surveys => {:survey_id => 1}, :kpis => {:kpi_id => 2}) 
+0

謝謝,這將是正確的查詢:'Question.includes(:調查:關鍵績效指標)。凡(:調查=> {:ID => 1},::kpis => {:id => 1})' – John