可以在单一模式“belong_to”不止一个父模型?
-
20-09-2019 - |
题
就像在计算器上,在我的应用程序,用户可以写问题,也可以提供答案:
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
我的问题与上述应答模型做:
它是确定一个Answer
到belong_to
的用户和问题的模式?
我有一种感觉,我读的地方,一个模型只能有一个外键。如果是这样,我怎么纠正呢?
解决方案
是的,完全是可以的,你将有有许多作为belongs_to的域模型变得更加复杂很多车型。我不知道你会读到一个模型只能有一个外键。
不隶属于 StackOverflow