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

Laravel - Eager Loading get only first result

I need to get all the user from my database excluding only the user that belongs to Admin and Staff group and I need, for each of them, to get their profile so I used the Eager Loading: $data['users'] = User::with('profile')->join('role_user',…
Christian Giupponi
  • 7,408
  • 11
  • 68
  • 113
0
votes
1 answer

Laravel: Eager loading with 3 "relations"

I've got an Album model. An Album can have many Event and many Picture but an Event belongs to one Album and one Picture belongs to one Album too. I'm trying to make a list of events. In each event, I would like to display album's title and 3 random…
Flobesst
  • 1,281
  • 1
  • 16
  • 26
0
votes
1 answer

laravel 4.0 : Eager loading error - Trying to get property of non-object while selecting condition

I am trying to display product on the category, and I use eager loading. how i can get it? my table product => product_id | productCategory_id | etc category => category_id | name_category productImage => productImage_id | product_id |…
0
votes
1 answer

How to lazy-load or eager-load the sum of associated table?

We build some object in our controller: @sites = Site.find(:all, :conditions => conditions, :order => 'group_id ASC') And then in the view (currently), we are doing something like: @sites.each do |site| %p Site Name = site.name -…
notaceo
  • 1,093
  • 10
  • 28
0
votes
2 answers

How to count eloquent relationship without the N+1 issue and loading the entire model?

I am showing a list of categories, and the article count within each category. I get the expected result, but I am having the N+1 problem. My CategoriesController index function: public function index() { return View::make('categories.index', [ …
Thomas Jensen
  • 2,138
  • 2
  • 25
  • 48
0
votes
1 answer

Eloquent eager loading acting as explicit join

I am building a small blog and I would like to use the built-in Eloquent eager loading, but I would like it to act as an explicit join. Here's what I'm trying to do, using a join, which works, but not in the way I want. $posts = Post::join('users',…
0
votes
2 answers

How to write the following as an eager loaded query in Rails 4.1?

So I am using the geocoder gem in Rails 4.1 and have the following models setup. The code works but the Bullet gem is detecting this line places.select { |place| @countries.places.include?(place.country) } as an N+1 query. Is there a good…
Nona
  • 5,302
  • 7
  • 41
  • 79
0
votes
1 answer

Laravel 4 eager loading an aggregate SUM not working - query running but not returning?

I've got a simple Eloquent model (Player), and I simply want to add a bit of data to the returned data - the sum of all points awarded to the player in question Here's what I'm doing:
ArkadePaulG
  • 161
  • 2
  • 10
0
votes
1 answer

How to use eager loading here, i mean just by using one variable instead of two. So i don't need to run two separate queries

1) How can use eager loading here, i mean just by using $SponceringUser instead of $ProfileUser. so i don't need to run two separate queries. if i exchange my $profileUser variable with $SponceringUser where in the code i should save it to get the…
Maggi
  • 173
  • 2
  • 4
  • 14
0
votes
1 answer

Pagination and Eager Loading in Laravel

My Controller method is: $topics=Topic::with(array('subtopics'=>function($query){ $query->orderBy('glc_subtopic_priority','asc'); }))->with('subtopics.resources')->find($id)->paginate(1); return…
Sonali Gupta
  • 288
  • 1
  • 5
  • 17
0
votes
3 answers

Counting total distant relationships in Laravel (eager loading?)

I'm having issues getting a proper count total with my Laravel model. Model Structure User Item ItemLike A user can have multiple Items, and each of these Items can have multiple ItemLikes (when a user 'likes' the item). I can easily get the…
Nick Carson
  • 678
  • 1
  • 4
  • 11
0
votes
1 answer

ActiveRecord Query to retrieve all object attributes including associations

I want to be able to make an ActiveRecord call (or anything else that might get me the right results) that will pass me all object attributes. For example: I have a contact model, which can have many "CustomField"'s and many…
emag3m
  • 75
  • 5
0
votes
1 answer

Eager loading of activerecord-reputation-system

I have a Post model that is using Twitter's activerecord reputation system to track votes: Post model: has_reputation :votes, source: :user, aggregated_by: :sum I am attempting to eager load the reputation associate with all posts on an index page.…
dmt2989
  • 1,610
  • 3
  • 17
  • 30
0
votes
2 answers

Laravel - Retrieving specific column from a releted query

I have 4 tables: conversations - id (pk) - userId1 (fk) - userId2 (fk) users - id (pk) - name - surname . . . - roleId (fk) - userStatusId (fk) roles - id (pk) - type (fk) user_status - id (pk) - description (fk) this are my models: class…
ciccioassenza
  • 233
  • 1
  • 7
  • 17
0
votes
1 answer

Ordering of nested relation by attribute with laravel

Hi there i'm trying to sort a collection by attribute of the relation. This is my model class Song extends \Eloquent { protected $fillable = ['title', 'year']; public function artist(){ return $this->hasOne('Artist','id',…
simon
  • 3,378
  • 2
  • 22
  • 32