简体   繁体   English

使用 has_many:through 进行 rails 关联急切加载

[英]rails association eager loading with has_many :through

I have trouble finding a way to link to class A to E. Here are the class implied:我很难找到一种方法来链接到 class A 到 E。这里是 class 隐含的:

A has many B A有很多B
B belongs to A B属于A
B belongs to C B属于C
A has many C through B A 有很多 C 到 B

C has many D C有很多D
D belongs to C D属于C
D belongs to E class_name C D 属于 E 类名 C
C has_many E through D C has_many E 到 D

Ok I know it is confusing so I made a schema and I hope it is more comprehensible...好的,我知道这很令人困惑,所以我制作了一个模式,我希望它更容易理解......

在此处输入图像描述

My question is how can I link the class A with the class E (that is in reality class C with a particula role).我的问题是如何将 class A 与 class E 联系起来(实际上是 class Z0D61F8370CAD1D412F8ZB8E5 与一个部分角色。) How can I use a.es (es => plural of E:s )?如何使用 a.es (es => E:s 的复数)?

I tried A has_many E through C (in my mind it was a good solution because A has_many C (through B) and C has_many E (through D)) but it raise an error:我尝试了 A has_many E 到 C (在我看来这是一个很好的解决方案,因为 A has_many C (通过 B)和 Z0D61F8370CAD1D412F8370CAD1D412F80B84D143E1257Z 但它引发了错误(通过_)

ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source reflection macro:has_many:through for has_many:es, :through =>:cs. ActiveRecord::HasManyThroughSourceAssociationMacroError: 无效的源反射宏:has_many:through for has_many:es, :through =>:cs。 Use:source to specify the source reflection使用:source 指定源反射

I'm coding on rails V2.3.9我在 Rails V2.3.9 上编码

If you have any idea please tell me.如果您有任何想法,请告诉我。 Thank you in advance.先感谢您。

Although there are some plugins that extend the :through relationship functionality, it's not generally possible to use ActiveRecord to navigate through multiple layers at the same time.尽管有一些插件可以扩展:through关系功能,但通常不可能使用 ActiveRecord 同时导航多个层。 The limit is generally one :through , no more.限制通常是一个:through ,没有更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM