Questions tagged [eager-loading]

Eager loading is a way to load objects of a certain class and a number of named associations in the same request.

When you load records from the database and also want to access the associated objects for each of these records, it’s a good idea to make use of eager loading. Eager loading reduces the amount of queries made to the database and therefore increases performance.

This is one of the easiest ways of to prevent the dreaded 1+N problem in which fetching 100 posts that each need to display their author triggers 101 database queries. Through the use of eager loading, the 101 queries can be reduced to 2.

# Rails 2
Blogpost.all(:include => :comments)

# Rails 3
Blogpost.includes(:comments).all

# Entity Framework
Blogpost.Include(bp => bp.Comments);
1515 questions
10
votes
3 answers

nhibernate, eager loading and paging

I am creating an mvc application that uses nhibernate and paging. I have a parent > child relationship that I am trying to eager load my child records. This is all working fine. The problem I am having is with the paging. I would like to have 15…
czuroski
  • 4,316
  • 9
  • 50
  • 86
10
votes
4 answers

Order by in nested eager loading in sequelize not working

i have four model Tehsil, Ilr, Patwar, and Villages. and their association is Tehsil -> 1:m -> Ilr -> 1:m -> Patwar -> 1:m -> Villages i want to to apply order by on all four of my models. Query: var tehsilQuery = { include: [{ model:…
pirate
  • 193
  • 1
  • 2
  • 14
10
votes
4 answers

"Unable to autoload constant User" error when changed code in development

I have a problem with my rails application. After an Update from Rails 3 to 4. When I surf through the pages after starting the server in development mode everything is fine. But after a single code change (even adding a space) every page request…
Sandro L
  • 1,140
  • 18
  • 32
10
votes
1 answer

Grails / GORM: difference between lazy: false & fetchMode eager

In Grails / GORM, what is the difference between static mapping = {xyz lazy: false} & static fetchMode = [xyz: 'eager']? Example: class Book { static belongsTo = [author: Author] static mapping = {author lazy: false} static fetchMode…
XDR
  • 4,070
  • 3
  • 30
  • 54
10
votes
3 answers

Rails 4.2 Autoloading not thread-safe

I have the following model: class User < ActiveRecord::Base def send_message(content) MessagePoro.new(content).deliver! end def self.send_to_all(content) threads = [] all.each do |user| threads << Thread.new do …
Benedikt B
  • 733
  • 8
  • 23
10
votes
1 answer

Ruby on Rails: :include on a polymorphic association with submodels

When working with a polymorphic association, is it possible to run an include on submodels that are only present in some types? Example: class Container belongs_to :contents, :polymorphic => true end class Food has_one :container belongs_to…
10
votes
3 answers

ActiveRecord eager load multiple belongs_to associations

The problem I have the following ActiveRecord model: class Person belongs_to :favourite_car, class_name: 'Car' belongs_to :business_car, class_name: 'Car' belongs_to :home_car, class_name: 'Car' end When I want to access all three of these…
Cameron Martin
  • 5,952
  • 2
  • 40
  • 53
10
votes
3 answers

rails polymorphic with includes based on type of class

Let's say we have these models class Message belongs_to :messageable, polymorphic: true end class Ticket has_many :messages, as: :messageable has_many :comments end class User has_many :messages, as: :messageable has_many…
10
votes
2 answers

EF Code First: Include not working on optional relationship

I have a specific query in my code which needs to eager load all related entities (both ->1 FKs and ->N FKs) because the context will be disposed right after that. I made a generic "Query" method that takes params Expression>[]…
Matteo Mosca
  • 7,380
  • 4
  • 44
  • 80
9
votes
3 answers

Entity Framework - eager loading of related entities

Sorry the title isn't more specific - I didn't know how to describe this succinctly. I have Trips and Location that have a many-to-many relationship - straightforward except that Locations have no need to know about the Trips that use them. I've…
gruve
  • 533
  • 3
  • 11
9
votes
2 answers

Forcing eager-loading for a navigation property

I'm using EF Code First and I have a navigation property called Category that I want eager loaded in every call: public class Product { ... public Category Category { get; set; } } To do this I have to include it in every call I'll do on…
Anderson Fortaleza
  • 2,449
  • 20
  • 22
9
votes
1 answer

Rails exclude folder from auto/eager load

I want to scale separately some subparts of my rails app and avoid loading the whole codebase. For the sake of example, let's consider an APIv1 vs an APIv2, but I'd also want to extend this any class/service in general Is it possible to exclude a…
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
9
votes
1 answer

Call to a member function addEagerConstraints() on integer

I tried to eager load a relation: $tournaments = Tournament::with('numCompetitors')->latest()->paginate(config('constants.PAGINATION')); My relation in Tournament returns an integer: public function numCompetitors() { return…
Juliatzin
  • 18,455
  • 40
  • 166
  • 325
9
votes
3 answers

Rails 4 - Select using eager_load

I'm trying to select only certain columns using eager_load, but I'm facing the problem that it cancels my 'select'. Model: class Timeline < ActiveRecord::Base belongs_to :timeline_category, foreign_key: :timeline_category_id belongs_to…
developer033
  • 24,267
  • 8
  • 82
  • 108
9
votes
2 answers

Eager load hasMany & belongsTo (circular reference/infinite loop)

UPDATE (SOLUTION) If you need ->user relationship from one of the $image inside $user->images, then $user variable is already available cause you loaded the ->images from it! Don't use protected $with Eloquent property. It's an…
eightyfive
  • 4,601
  • 3
  • 35
  • 44