David
David

Reputation: 825

Eager load Rails 4 - still multiple queries

I have this code:

class User < ActiveRecord::Base
   has_many :people
end

class Person < ActiveRecord::Base
   belongs_to :user
end

def map_people people
   people.map {|person|
     map_person(person)
   }
end

def map_person person
   {
     :person_id => person.id,
     :user_id => person.user.id,
     :name => person.user.name
   }
end

and when I run:

map_people(Person.with_deleted.includes(:user))

I'm getting

 Person Load (1.4ms)  SELECT `people`.* FROM `people`
 User Load (2.5ms)  SELECT `users`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`id` IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 76, 78, 79, 80, 81, 83, 84, 85, 87, 88, 89, 90, 91, 92, 75, 93, 94, 95, 96, 97, 98, 99, 100, 101, 104, 105, 106, 107, 108, 109, 112, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 127, 128, 135, 142, 143)  ORDER BY users.updated_at DESC
 User Load (0.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1  ORDER BY users.updated_at DESC LIMIT 1
 User Load (0.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 2  ORDER BY users.updated_at DESC LIMIT 1
 User Load (0.5ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 3  ORDER BY users.updated_at DESC LIMIT 1
 User Load (0.6ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 3  ORDER BY users.updated_at DESC LIMIT 1
 User Load (0.6ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 2  ORDER BY users.updated_at DESC LIMIT 1
 User Load (0.6ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 2  ORDER BY users.updated_at DESC LIMIT 1
 User Load (0.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1  ORDER BY users.updated_at DESC LIMIT 1

and and lot more SELECT 'users'.* FROM ..... queries

Hos is possible to load all with one query?

Thank you!

David

Upvotes: 2

Views: 315

Answers (3)

David
David

Reputation: 825

Sorry guys, my mistake (class Person):

  def user
     @user ||= user_id ? User.with_deleted.find(user_id) : nil
  end

so it always loaded again

Upvotes: 0

Andre Ogle
Andre Ogle

Reputation: 172

I would try do this using eager_load. This will pull all people into memory first (using one query) and then you can iterate over each one, calling the map_person method each time.

This should work:

def map_people
  all_people = Person.eager_load(:user).with_deleted.to_a
  all_people.map { |person| map_person(person) }
end

You could also pass in the people array to the method if you wanted to, like this:

def map_people(people)
  people.map { |person| map_person(person) }
end

all_people = Person.eager_load(:user).with_deleted.to_a
map_people(all_people)

Upvotes: 2

Yury Lebedev
Yury Lebedev

Reputation: 4015

Try running

map_people(Person.with_deleted.preload(:user))

There is a nice article on this topic btw:

http://blog.arkency.com/2013/12/rails4-preloading/

Upvotes: 0

Related Questions