棘手的问题答案联想? [英] Tricky Questions Answers associations?
问题描述
所以,我有3个型号。
有一个用户模式,问题模型和应答模式。
A User model, a Questions model and an Answer model.
一个用户的has_many问题,问题belong_to用户
A user has_many questions, and questions belong_to user
一个问题HAS_ONE答案,belong_to问题的答案。
A question has_one answer, and the answers belong_to the question.
这适用于所有用户现在我已经创建默认种子问题,即@questions = Question.all
Now I've created default seed questions that apply to all users i.e. @questions = Question.all
和每个用户都可以看到这些同样的问题,现在我怎么能允许每个用户自己的答案写这些问题的时候都不会直接与问题有关的?
And these same questions every user can see, now how can I allow each user to write their own answer to these questions when they aren't directly associated with the question?
即。 u.questions.answer返回答案是不确定的。
I.e. u.questions.answer returns answer is undefined.
推荐答案
我推荐一个具有通过联合众多,在中间表的链接答案。因此:
I recommend a has many through association, with a link to the answer in the intermediate table. Thus:
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
在这种方式,你可以创建可以与用户相关联与否,也链接到他们的回答,在这里存在的问题。
In this way, you can create questions that you can associate with a user or not, and also link to their answer, where it exists.
这篇关于棘手的问题答案联想?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!