简体   繁体   中英

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:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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