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
0
votes
1 answer

Entity Framework - Shallow Eager Loading?

Is it possible to eagerly load related entities, but to not have the related entities of the related entities be loaded? In my case, I have a set of flags to determine which related entities should be loaded: [Flags] public enum…
0
votes
1 answer

Why does the where() method run SQL queries after all nested relations are eager-loaded?

In my controller method for the the index view I have the following line. @students_instance = Student.includes(:memo_tests => {:memo_target => :memo_level}) So for each Student I eager-load all necessary info. Later on in a .map block, I call the…
nonsequiter
  • 663
  • 3
  • 9
  • 22
0
votes
1 answer

Eager loading sum from two pivot tables with Eloquent

I have a Part entity that has a many-to-many relationship with Order and Project. The pivot tables contains a quantity field which might have a positive or negative value, and by adding the sums of both pivot tables, for a specific part, I get the…
Thomas Jensen
  • 2,138
  • 2
  • 25
  • 48
0
votes
0 answers

Laravel Eloquent Eager Loading multiple relationships with pagination

I have a site running Laravel 4.2 that I am working on right now, the problem is that I have a single model for orders and another child model for items that belong to the order. The items also have a child model for size information and image…
Tom Bird
  • 999
  • 3
  • 17
  • 31
0
votes
1 answer

How to eagerly load multiple related models in Laravel 5?

I have a relationships as follows: An Order has many Products, and a Product has many Orders. class Order extends Model { ... public function products() { return $this->belongsToMany('App\Product'); } } A Product has many…
John Bupit
  • 10,406
  • 8
  • 39
  • 75
0
votes
3 answers

How can I check associated attributes in Rails?

I have the following relationships User has_many Clients through ClientMemberships Client has_many Projects Project belongs_to Client Given a specific user. What is the best way to find all clients for a user which have a specific project ID. I…
Eki Eqbal
  • 5,779
  • 9
  • 47
  • 81
0
votes
1 answer

CakePHP 3 - Nested Eager Loading Error

I have 3 tables with many to many relationship: users groups tools And I have 3 pivot tables for the relationships: groups_users tools_users groups_tools I'm trying to get the User -> tools, and the User -> Group -> Tools: $user =…
0
votes
1 answer

Multiple where clause with include ActiveRecord Rails

I have two tables "patients" and "patient_funds" patient: id status 1 active 2 active 3 inactive patient_funds: id required allocated patient_id 1 10 5 1 2 10 10 2 I want to…
rmn.nish
  • 871
  • 2
  • 8
  • 24
0
votes
1 answer

Rails Nested Eager Loading?

Lets say we have a nested set up where class Foo belongs_to :object has_many :bar end class Bar belongs_to :foo has_one :abc end class Abc belongs_to :bar end And I want to eager load abc from foo. What would be the best way to do…
aMat
  • 69
  • 7
0
votes
1 answer

How to load all related EF entities in preparation for serialization

Can someone tell me how to (elegantly) load all related entity objects in preparation for serialization to disk? I have a table Session. A Session can have multiple Recordings which are in the Recordings Table. Further, each Recording can have…
Dave
  • 8,095
  • 14
  • 56
  • 99
0
votes
2 answers

custom orderBy() on constraining eager loads?

my question is about the possibilty of customizing Laravel's orderBy() method of the query builder, which i am using to sort an eager loaded dataset. This is the query scope I am using to generate the dataset. Everything works fine so far with this.…
manpenaloza
  • 385
  • 1
  • 3
  • 13
0
votes
2 answers

Eager Loading Multiple Associations and serialization

In my project I have 3 models Assignment, Question and MultipleChoice with the following associations assignment.rb has_many :questions, dependent: :destroy question.rb belongs_to :assignment, class_name: 'Assignment', foreign_key: :assignment_id …
user1670773
0
votes
2 answers

Rails: eager load related model with condition at third model

I am very stuck on this problem: I have next models (it's all from Redmine): Issue (has_many :journals) Journal (has_many :details, :class_name => "JournalDetails) JournalDetails I want fetch last changing of a state of a issue: date saved in…
kunashir
  • 917
  • 8
  • 21
0
votes
1 answer

Parent object eager load nested associations Rails

In Rails 4 i have the following models class Parent < ActiveRecord::Base has_many :sons has_many :grand_sons, through: sons scope :load_tree, (id) -> {Parent.includes(sons: [:grand_sons]).find(id)} end class Son < ActiveRecord::Base …
0
votes
0 answers

Web Api Returning Json - [System.NotSupportedException] Specified method is not supported. (Sybase Ase)

I'm using Web api with Entity Framework 4.2 and the Sybase Ase connector. This was working without issues returning JSon, until I tried to add a new table. return db.car .Include("tires") .Include("tires.hub_caps") …
trees_are_great
  • 3,881
  • 3
  • 31
  • 62