gpt4 book ai didi

ruby-on-rails - Rails 中的双多态关联

转载 作者:搜寻专家 更新时间:2023-10-30 21:36:25 24 4
gpt4 key购买 nike

我有一个连接表,我希望 belongs_to 关联都是多态的。关系如下所示:

Double Polymorhpic

类结构如下所示:

Class Tagable1 < ActiveRecord::Base
has_many :taggings
has_many :tag_group_1s, :through => :taggings
has_many :tag_group_2s, :through => :taggings
...
end

Class TagGroup1 < ActiveRecord::Base
has_many :taggings
has_many :taggable_1s, :through => :taggings
has_many :taggable_2s, :through => :taggings
...
end

Class Tagging < ActiveRecord::Base
belongs_to :taggable, :polymorphic => true
belongs_to :tag, :polymorphic => true
end

这种方法有明显的缺点,尤其是标记表上的负载很重。我想要实现的是一种灵活的设计,它允许我创建新的 Taggables 或 TagGroups 而无需创建新的连接表。例如,如果没有双重多态性,Taggable1 将需要一个 Taggable1Taggings 表来将 Taggable1 与多态性 TagGroups 连接起来。同样,我需要 Taggable2Taggings、Taggable3Taggings、Taggable4Taggings 等。

如果有我没有想到的缺点,我很乐意得到启发。

最佳答案

我不确定它是否与 Rails 3 兼容,但您需要 has_many_polymorphs gem:

https://github.com/fauna/has_many_polymorphs

这个分支声称与 Rails 3 兼容:

https://github.com/jystewart/has_many_polymorphs

关于ruby-on-rails - Rails 中的双多态关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7680338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com