可能有"多态型HAS_ONE"在导轨的关系? [英] Possible to have "polymorphic has_one" relationship in rails?
问题描述
我希望做这样的事情:
Category
--------
- id
- name
Tag
--------
- id
- tag
Campaign
--------
- id
- name
- target (either a tag *or* a category)
时的关联是这里的答案吗?我似乎无法弄清楚如何使用它HAS_ONE:目标:为=>:靶向
Is a polymorphic association the answer here? I can't seem to figure out how to use it with has_one :target, :as => :targetable.
基本上,我希望Campaign.target被设置为一个标签或一个类别(或潜在另一个模型在未来)。
Basically, I want Campaign.target to be set to a Tag or a Category (or potentially another model in the future).
推荐答案
我不相信你在这里需要一个 HAS_ONE
关联,则 belongs_to的
应该是你正在寻找的东西。
I don't believe you're in need of a has_one
association here, the belongs_to
should be what you're looking for.
在这种情况下,你会希望有一个 target_id
和 TARGET_TYPE
在广告系列的表列中,您可以创建这些与一个耙t.references:目标
通话(其中 T
是表
变量)。
In this case, you'd want a target_id
and target_type
column on your Campaign table, you can create these in a rake with a t.references :target
call (where t
is the table
variable).
class Campaign < ActiveRecord::Base
belongs_to :target, :polymorphic => true
end
现在的运动可以关联到一个标签
或类别
和 @campaign。目标
将返回一个合适的。
Now campaign can be associated to either a Tag
or Category
and @campaign.target
would return the appropriate one.
在 HAS_ONE
的关联将被使用,如果你有目标表指向回到你的活动外键
The has_one
association would be used if you have a foreign key on the target table pointing back to your Campaign
.
例如,您的表将有
标签:ID,标签,campaign_id
类别:编号,类别,campaign_id
和将有一个 belongs_to的:他们两个活动
的关联。在这种情况下,你必须使用 HAS_ONE:标签
和 HAS_ONE:类
,但你不能使用一个普通的目标
在这一点上。
and would have a belongs_to :campaign
association on both of them. In this case, you'd have to use has_one :tag
and has_one :category
, but you couldn't use a generic target
at this point.
这是否更有意义?
修改
由于 target_id
和 TARGET_TYPE
是有效的外键到另一个表,你的广告系列
属于其中之一。我可以看到你的困惑的写法,因为在逻辑上的广告系列
的容器。我想你可以把它看成是广告系列
有一个单一的目标,这是一个标签
或容器
,因此它属于一个标签
或容器
。
Since target_id
and target_type
are effectively foreign keys to another table, your Campaign
belongs to one of them. I can see your confusion with the wording because logically the Campaign
is the container. I guess you can think of it as Campaign
has a single target, and that's a Tag
or a Container
, therefore it belongs in a Tag
or Container
.
在 HAS_ONE
是说的关系在目标类中定义的方法。例如,标签
将通过一个 HAS_ONE
的关系,因为没有什么标签上的类相关联的运动,标识的关联。在这种情况下,你必须
The has_one
is the way of saying the relationship is defined on the target class. For example, a Tag
would have be associated to the campaign through a has_one
relationship since there's nothing on the tag class that identifies the association. In this case, you'd have
class Tag < ActiveRecord::Base
has_one :campaign, :as => :target
end
和同样为类别
。在这里,:为
关键字告诉导轨其关联涉及回本标签
。 Rails的不知道怎么算出这个前期,因为有这个名字没有关联标记
在广告系列
。
and likewise for a Category
. Here, the :as
keyword is telling rails which association relates back to this Tag
. Rails doesn't know how to figure this out upfront because there's no association with the name tag
on the Campaign
.
另外两个选项,可以提供进一步的混乱是源
和 SOURCE_TYPE
选项。这些选项只用在:通过
的关系,在这里你实际上是通过另一个表加入该协会。该文档可能更好地描述它,但
源
定义的关联名称, SOURCE_TYPE
被用在该协会是多态。他们只需要使用当目标协会(在:通过
类)都有一个名字,是不是显而易见的 - 像上面用目标和
标签 - 我们需要告诉Rails要使用哪一个
The other two options that may provide further confusion are the source
and source_type
options. These are only used in :through
relationships, where you're actually joining the association through
another table. The docs probably describe it better, but the source
defines the association name, and source_type
is used where that association is polymorphic. They only need to be used when the target association (on the :through
class) has a name that isn't obvious -- like the case above with target and
Tag -- and we need to tell rails which one to use.
这篇关于可能有&QUOT;多态型HAS_ONE&QUOT;在导轨的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!