- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个连接表,我希望 belongs_to 关联都是多态的。关系如下所示:
类结构如下所示:
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 兼容:
关于ruby-on-rails - Rails 中的双多态关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7680338/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!