的has_many与外键存储在自己的表 [英] has_many with foreign key stored in own table

查看:96
本文介绍了的has_many与外键存储在自己的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆