我正在使用Rails 4来构建问答类型的应用程序。
我创建的模型应该是:
Question
belongs_to :user
has_many :answers
Answer
belongs_to :user
belongs_to :question
或者应该是:
Question
QuestionAnswer
它基于您选择表名称的偏好,但是具有简单questions
和answers
表是可读的,并且在您调用诸如sayQuestion.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
但是,如何建模数据实际上取决于您。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句