Skip to content
Advertisement

Rails – deeply nested joins with ActiveRecord

I occasionally need to do a join in my app that goes across multiple tables, but haven’t quite figured out how to do this for more than 3 tables:

Currently I’m ready to go:

What I would like to do now is to join every consumer that belongs to an OrderListItem.

However, when I try the following:

I get the error:

With the following two attempts also the same error:

I have set the associations correctly and order_list_item.consumers and consumer.order_list_items and order_list_item.product_variant as well as product_variant.order_list_items work fine.

I have also not overridden initialize on any model.

The working SQL looks like this:

Thanks for any tips.

Advertisement

Answer

Try this

Below is the sql query

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement