与 Rails/ActiveRecord 的多态 habtm 关系
我将如何与 Rails/ActiveRecord 创建多态 has_and_belongs_to_many 关系?
我看到的大多数示例都涉及创建一个 own_to 关系,该关系将我的多态端限制为仅与一个父级相关:
表:任务
表:Tasks_Targets
表:CustomerStore
表:SoftwareSystem
在此,CustomerStore 和 SoftwareSystem 都属于“Targetable”类型环境。 据我了解,如果我实现大多数示例所示的多态关系,我只能将目标与任务关联一次。
一些澄清可能会有所帮助,因为大多数在线搜索仍然无法解释这种关系背后的一些理论......
谢谢!
How would I go about creating a polymorphic has_and_belongs_to_many relationship with Rails/ActiveRecord?
Most of the examples I see involve creating a belongs_to relationship which limits my polymorphic-side to being related to only one parent:
Table: Task
Table: Tasks_Targets
Table: CustomerStore
Table: SoftwareSystem
Both CustomerStore and SoftwareSystem would be of type "Targetable" in this circumstance. From what I understand, if I implement the polymorphic relationship as most examples show, I'd only be able to relate a Targetable to a Task once.
Some clarification might help as most searches online still leave some of the theory behind this relationship unexplained...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您对领域的解释,我制作了一个小型测试驱动示例,说明如何解决您的问题。 如果您发现任何域名不一致的地方,请随时进一步澄清(我正在使用我的
acts_as_fu gem
即时启动测试模型)。Given your explanation of your domain, I've whipped up a small test-driven example of how you might solve your problem. If you see any domain inconsistencies, please feel free to clarify further (I'm using my
acts_as_fu gem
to whip up test models on the fly).为了补充 nakajima 对您的担忧的回答,我将这样做:
确保在表
task_targets
中索引task_id
。To complement nakajima's answer with regards to your concern, here's how I would do it:
Make sure to index
task_id
in tabletask_targets
.