查询双的has_many:通过关联
问题描述:
我有一个双重的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
问题,但我不知道该如何查询这个?
答
嘿,你可以尝试这样
Question.includes(:surveys,:kpis).where(:surveys => {:survey_id => 1}, :kpis => {:kpi_id => 2})
谢谢,这将是正确的查询:'Question.includes(:调查:关键绩效指标)。凡(:调查=> {:ID => 1},::kpis => {:id => 1})' – John