belongs_to through associations

前端 未结 7 852
自闭症患者
自闭症患者 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:09

    So you cant have the behavior that you want but you can do something that feels like it. You want to be able to do Choice.first.question

    what I have done in the past is something like this

    class Choice
      belongs_to :user
      belongs_to :answer
      validates_uniqueness_of :answer_id, :scope => [ :question_id, :user_id ]
      ...
      def question
        answer.question
      end
    end
    

    this way the you can now call question on Choice

提交回复
热议问题