belongs_to through associations

前端 未结 7 875
自闭症患者
自闭症患者 2020-11-30 17:19

Given the following associations, I need to reference the Question that a Choice is attached through from the Choice model. I have bee

7条回答
  •  囚心锁ツ
    2020-11-30 18:07

    It sounds like what you want is a User who has many Questions.
    The Question has many Answers, one of which is the User's Choice.

    Is this what you are after?

    I would model something like that along these lines:

    class User
      has_many :questions
    end
    
    class Question
      belongs_to :user
      has_many   :answers
      has_one    :choice, :class_name => "Answer"
    
      validates_inclusion_of :choice, :in => lambda { answers }
    end
    
    class Answer
      belongs_to :question
    end
    

提交回复
热议问题