所以我有3个模特..
用户模型,问题模型和答案模型。
用户有很多问题,问题属于用户
一个问题has_one回答,答案属于问题。
现在我已经创建了适用于所有用户的默认种子问题,即@questions = Question.all
每个用户都可以看到这些相同的问题,现在,如果这些问题与这些问题没有直接关联,我怎样才能允许每个用户写下自己的答案呢?
即。 u.questions.answer返回答案未定义。
答案 0 :(得分:0)
我建议有很多通过关联,在中间表中有一个指向答案的链接。因此:
class User < ActiveRecord::Base
has_many :user_questions
has_many :questions, through: :user_questions
end
class UserQuestion < ActiveRecord::Base
belongs_to :user
belongs_to :question
belongs_to :answer
end
通过这种方式,您可以创建可以与用户关联的问题,还可以链接到他们的答案。