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
The above models are inherited from the base model Exam
(using rails STI) and the join table contains two fields: question_id
and question_set_id
. Now I need to convert this association into has_many through
.
I have tried as follows:
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