就像在计算器上,在我的应用程序,用户可以写问题,也可以提供答案:

class User < ActiveRecord::Base
  has_many :questions
  has_many :answers
end

class Question < ActiveRecord::Base
  has_many :answers
  belongs_to :user
end


class Answer < ActiveRecord::Base
  belongs_to :question
  belongs_to :user
end

我的问题与上述应答模型做:

它是确定一个Answerbelong_to的用户和问题的模式?

我有一种感觉,我读的地方,一个模型只能有一个外键。如果是这样,我怎么纠正呢?

有帮助吗?

解决方案

是的,完全是可以的,你将有有许多作为belongs_to的域模型变得更加复杂很多车型。我不知道你会读到一个模型只能有一个外键。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top