如果使用belongs_to的不具有相应的HAS_ONE会发生什么? [英] what would happen if you use belongs_to without having a corresponding has_one?
问题描述
我有一个核心模型为项目
,以及一个脚本来从自定义源填充它(认为,RSS提要)。在饲料中,每个项目都被标识一个的GUID
;在我的系统,项目只有一个自动生成的 ID
主键。
I have a core model for an Item
, and a script to populate it from a custom source (think, RSS feed). In the feed, each Item is identified with a guid
; in my system, Item only has an autogenerated id
primary key.
我想有,比方说, ItemFeedInfo
的地图 guid-> ID
(这样我就可以新主场迎战修改的项目区分)
I want to have, let's say, ItemFeedInfo
that maps guid->id
(so that I can distinguish between new vs. modified Items)
我想创造
class ItemFeedInfo
belongs_to :Item
end
我会preFER不修改项目
自定义在逻辑上是独立的 ItemFeedInfo
然而,每例如,我可以找到使用 belongs_to的
中,提到了一个 HAS_ONE
对应。需要它?
I would prefer not to modify Item
since its definition is logically independent of ItemFeedInfo
However, every example I can find of using belongs_to
, mentions a has_one
counterpart. Is it required?
推荐答案
没关系。
belongs_to的
只是增加了一些方法,在 ItemFeedInfo
类。当你这样做,你知道 item_feed_info.items.find_all
...的项目
只是它得到了动态添加时的方法你叫 belongs_to的
。
belongs_to
simply adds some methods to the ItemFeedInfo
class. You know when you do item_feed_info.items.find_all
... The items
is just a method which got dynamically added when you called belongs_to
.
如果您不修改项目
类,那么你根本不会再增加对对方的方法。你不会是能够做到 item.item_feed_infos.find_all
,因为没有添加的has_many:item_feed_infos
,该方法韩元'吨已被添加到项目
类,但如果你从来没有真正需要这样做,都不会有问题。
If you don't modify the Item
class, then you simply won't be adding the methods on the other side. You won't be able to do item.item_feed_infos.find_all
, because without adding has_many :item_feed_infos
, that method won't have been added to the Item
class, but if you never actually need to do this, it won't matter.
这篇关于如果使用belongs_to的不具有相应的HAS_ONE会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!