belongs_to through associations

前端 未结 7 851
自闭症患者
自闭症患者 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 17:52

    Just use has_one instead of belongs_to in your :through, like this:

    class Choice
      belongs_to :user
      belongs_to :answer
      has_one :question, :through => :answer
    end
    

    Unrelated, but I'd be hesitant to use validates_uniqueness_of instead of using a proper unique constraint in your database. When you do this in ruby you have race conditions.

提交回复
热议问题