2014-03-30 29 views
0

我有轨关系:允许用户使用accepting_nested_attributes_for选择正确答案?

class Quiz < ActiveRecord::Base 
    has_many :questions, dependent: :destroy 
    accepts_nested_attributes_for :questions, 
          reject_if: proc { |a| a[:content].blank? }, 
          allow_destroy: true 
end 

class Question < ActiveRecord::Base 
    belongs_to :quiz 
    has_many :answers, dependent: :destroy 
    accepts_nested_attributes_for :answers, 
           reject_if: proc { |a| a[:content].blank? }, 
           allow_destroy: true 
end 

class Answer < ActiveRecord::Base 
    belongs_to :question 
end 

怎样的结构模型,所以当用户在测验#编辑或测验#新的,他们可以选择哪个答案(含单选按钮)为正确答案?

回答

0

answers表中,添加名为'is_correct_answer'的附加属性,该属性仅适用于一个组合。

+0

我认为这是一个额外的连接,不需要,可以通过其他方式避免 –

+0

您可以在答案表本身做同样的事情,而无需额外的表格 – emaillenin