2
class QuestionSet 
    has_and_belongs_to_many :questions, 
         class_name: 'Exam', 
         join_table: 'question_question_sets', 
         foreign_key: 'question_set_id', 
         association_foreign_key: 'question_id' 

end 

class Question 
    has_and_belongs_to_many :question_sets, 
         class_name: 'Exam', 
         join_table: 'question_question_sets', 
         foreign_key: 'question_id', 
         association_foreign_key: 'question_set_id' 

end 

上述型號從基模型Exam繼承(使用導軌STI)和連接表包含兩個字段:question_idquestion_set_id。現在我需要將此關聯轉換爲has_many through導軌HABTM到有許多通過

我已經試過如下:

class QuestionQuestionSet 
    has_many :questions 
    has_many :question_sets 
end 

class Question 
    has_many :question_question_sets, foreign_key: :question_id 
    has_many :question_sets, through: :question_question_sets 
end 

class QuestionSet 
    has_many :question_question_sets, foreign_key: :question_set_id 
    has_many :questions, through: :question_question_sets 
end 
+0

中指示的步驟進行操作。請在您要求某人爲您做這件事之前,先顯示您嘗試過的方法。 –

+0

我正在玩類名和foreign_key。但沒有爲我工作 –

+0

更新我的問題,請看看它 –

回答