Skip to main content

Loading Associations of associations using joins instead of includes


Imagine a Rails scenario where,User model has many associations.So it make look like:-

Use includes for lazy loading of all associations
Use joins to load all associations that exists

For example:-
                 A user has many orders.
                 Order has many images,videos,comments,testimonials,feedbacks,address,contact_details and annoucements.

So,to access a user having a request,with all orders having all associations,we will use the usual includes like :-

User.includes( { orders: [ { :address,:contact_details,:comments,annoucements,feedbacks: [:videos, :images] }] }) will load all users .

But,to get all users having all associations present,we can use Joins like:

User.joins( { orders: [ { :address,:contact_details,:comments,annoucements,feedbacks: [:videos, :images] }] }) will load all users with associations present.



Hope it helps :)



Comments