Method: Mongoid::Criteria#includes

Defined in:
lib/mongoid/criteria.rb

#includes(*relations) ⇒ Criteria

Note:

This will work for embedded relations that reference another collection via belongs_to as well.

Note:

Eager loading brings all the documents into memory, so there is a sweet spot on the performance gains. Internal benchmarks show that eager loading becomes slower around 100k documents, but this will naturally depend on the specific application.

Eager loads all the provided relations. Will load all the documents into the identity map who’s ids match based on the extra query for the ids.

Examples:

Eager load the provided relations.

Person.includes(:posts, :game)

Parameters:

  • relations (Array<Symbol>)

    The names of the relations to eager load.

Returns:

Since:

  • 2.2.0



218
219
220
221
222
223
224
225
# File 'lib/mongoid/criteria.rb', line 218

def includes(*relations)
  relations.flatten.each do |name|
     = klass.reflect_on_association(name)
    raise Errors::InvalidIncludes.new(klass, relations) unless 
    inclusions.push() unless inclusions.include?()
  end
  clone
end