It looks like you want to use polymorphic relationships here. You will need a common name for the exam / quiz / assignment, and each question will belong to one of them. Let's say you call them "Ratings", you should set up your models as follows:
class Question << ActiveRecord::Base belongs_to :assessment, :polymorphic => true end class Exam << ActiveRecord::Base has_many :questions, :as => :assessment end class Quiz << ActiveRecord::Base has_many :questions, :as => :assessment end class Assignment << ActiveRecord::Base has_many :questions, :as => :assessment end
Then you need to add two fields to your question model:
assessment_id assessment_type
Using this link, you can use it as:
@exam = Exam.create({:field1 => :val1}) @exam.questions.create({:field1 => :question1}) @exam.questions.create({:field1 => :question2})
and he knows exactly which questions relate to which model, based on additional fields in your question model.
source share