的has_many与外键存储在自己的表 [英] has_many with foreign key stored in own table
问题描述
我的模型父
的 HAS_ONE
孩子
。然而,对于一些合乎逻辑的理由,我需要存储在父
而不是孩子
外键。
I have the model Parent
that has_one
Child
. However, for some logical reason, I need to store foreign key in Parent
instead of Child
.
是否有可能定义 HAS_ONE
与在父外键关系
表?
Is it possible to define has_one
relationship with foreign key in the Parent
table?
我不想定义父
belongs_to的
孩子
,因为我希望通过定义来创建父
这些对象 accepts_nested_attributes_for
。
I don't want to define Parent
belongs_to
Child
, because I want to create these objects from Parent
by defining accepts_nested_attributes_for
.
推荐答案
这是 HAS_ONE
的反 - 也就是 belongs_to的
。
That is the inverse of has_one
- that is belongs_to
.
请参阅的http://guides.rubyonrails.org/association_basics.html#choosing-between-belongs-to-and-has-one了解更多信息。
See http://guides.rubyonrails.org/association_basics.html#choosing-between-belongs-to-and-has-one for more information.
这篇关于的has_many与外键存储在自己的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!