Mongoid::Includes
Mongoid::Includes improves eager loading in Mongoid, supporting polymorphic associations, and nested eager loading.
Usage
Album.includes(:songs).includes(:musicians, from: :band)
Band.includes(:albums, with: ->(albums) { albums.gt(release: 1970) })
# The library supports nested eager loading using :from for terseness,
# but you can manually include nested associations using the :with option.
released_only = ->(albums) { albums.where(released: true) }
Musician.includes(:band, with: ->(bands) { bands.limit(2).includes(:albums, with: released_only) })
Advantages
- Avoid N+1 queries and get better performance.
- No boilerplate code is required.
- Can avoid loading all the related documents if necessary.
License
Copyright (c) 2015 M
