我使用Rails 4来构建问题并回答应用类型。
我创建的模型应该是:
Question
belongs_to :user
has_many :answers
Answer
belongs_to :user
belongs_to :question
或者它应该是:
Question
QuestionAnswer
由于
答案 0 :(得分:2)
这是基于您选择表名称的偏好,但简单地questions
和answers
表是可读的,当您调用相关方法(如Question.last.answers
而不是Question.last.question_answers
(没有重复)。
此外,QuestionAnswer
看起来更像是用于表示question_answers
关联上的联接模型has_many through
的内容。
# models/question.rb
# database table - questions
class Question < ActiveRecord::Base
belongs_to :user
has_many :answers
end
# models/answer.rb
# database table - answers (question_id as foreign key)
class Answer < ActiveRecord::Base
belongs_to :questions
end
但是,您需要如何为数据建模。