Mongoload
Mongoload is a gem to perform eager loading automatically for Mongoid, inspired by Goldiloader.
Usage
Just install Mongoload. It will automatically eager load your reference relation the first time you access it.
Install
Add this line to your application's Gemfile:
gem 'mongoload'
Or install it yourself as:
$ gem install mongoload
Options
:auto_include
Mark if Mongoload should perform eager loading on the relation.
Pass :auto_include
option to the definition, the value should be true
(by default) or false
.
class User
include Mongoid::Document
has_one :device, auto_include: false # Do not perform automatic eager loading on :device relation
has_many :posts
field :username
end
# Following call will not trigger automatic eager loading on device
User.all.each(&:device)
:fully_load
Mark if Mongoload should perform eager loading on the relation, when accessed by following methods:
- #first
- #last
- #size
- #empty?
Pass :fully_load
option to the definition, the value should be true
or false
(by default).
class Tag
include Mongoid::Document
has_and_belongs_to_many :posts, fully_load: true
field :name
end
# Following calls will trigger automatic eager loading on posts
Tag.all.each { |tag| tag.posts.first }
Tag.all.each { |tag| tag.posts.last }
Tag.all.each { |tag| tag.posts.size }
Tag.all.each { |tag| tag.posts.empty? }
PS: As for #second
, #third
, #fourth
, #fifth
and #forty_two
calls, mongoid will delegate them to the entries of a relation enumerable set, which makes them just same as a #to_a
call.
Copyright
Copyright (c) 2016 Kaloku Sang
See LICENSE.txt for details.