Rails 关联通过 has_many 进行预加载:through
我很难找到将 A 类链接到 E 类的方法。以下是隐含的类:
A 有许多 B
B 属于 A
B 属于 C
A 通过 B 有许多 C
C有很多D
D 属于 C
D 属于 E 类名 C
C has_many E 到 D
好吧,我知道这很令人困惑,所以我做了一个模式,我希望它更容易理解...
我的问题是如何将 A 类与 E 类(实际上是具有特定角色的 C 类)联系起来。如何使用 a.es (es => E :s 的复数) ?
我尝试了 A has_many E 到 C (在我看来这是一个很好的解决方案,因为 A has_many C (通过 B)和 C has_many E (通过 D)),但它引发了一个错误:
ActiveRecord::HasManyThroughSourceAssociationMacroError: 无效的源反射宏 :has_many :through 对于 has_many :es, :through => :CS。使用 :source 指定
我在 Rails V2.3.9 上编码的
源反射如果您有任何想法请告诉我。 先感谢您。
I have trouble finding a way to link to class A to E. Here are the class implied :
A has many B
B belongs to A
B belongs to C
A has many C through B
C has many D
D belongs to C
D belongs to E class_name C
C has_many E through 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). How can I use a.es (es => plural of 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 :
ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source reflection macro :has_many :through for has_many :es, :through => :cs. Use :source to specify the source reflection
I'm coding on rails V2.3.9
If you have any idea please tell me.
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管有一些插件扩展了
:through
关系功能,但通常不可能使用 ActiveRecord 同时导航多个层。限制通常为 1 个:through
,仅此而已。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. The limit is generally one:through
, no more.