Nik So
Nik So

Reputation: 16821

Mongoid: How to chain eager loading?

I have three models: Project, LineItem, and Spec

Project has many line items Project has many specs

I figured out how to eager load Project in a LineItem query:

LineItem.all.includes(:project)

but I can't seem to be able to get the eager loaded projects to also eager load their specs. How does one chain such eager loads in Mongoid?

Thank you!

Upvotes: 3

Views: 1924

Answers (3)

Victor Pudeyev
Victor Pudeyev

Reputation: 4538

mongoid_includes should answer that for you https://github.com/ElMassimo/mongoid_includes

Upvotes: 2

Cyril Duchon-Doris
Cyril Duchon-Doris

Reputation: 14029

Have a look at Rails doc here

LineItem.all.includes(:project => :specs)
# or
LineItem.all.includes({project: :specs})

Upvotes: -1

Baruch
Baruch

Reputation: 1133

If it is so important to be eager loading, perhaps you should consider embedding those specs. I know this is not an answer to your question, but it may be a better solution.

Upvotes: 1

Related Questions