Forum: Ruby on Rails Eager loading problem. Help greately appreciated

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cd8c9864d88bcafc164d8fdb820cc451?d=identicon&s=25 Chris (Guest)
on 2006-03-01 13:43
Each Timesheet has an employee.  An employee has a division and a
location.
I want to find all the timesheets with a status of 2.  I then iterate
over the timesheet collection and print the timesheet name, employee
name, employee divison name, and employee location name.  Like so:

for t in
Timesheet.find(:all,:conditions=>"status=2",:include=>:employee)
  puts timesheet.date
  puts timesheet.employee.name
  puts timesheet.employee.division.name
  puts timesheet.employee.location.name
end

Adding the ":include=>:employee" to the find preloads all the employees
BUT it does not preload the division and location.  This query returns
over 1000 timesheets, so it takes a long time to load each division +
location.

Any ideas how to preload the Division and Location??

Any help is really appreciated as I'm really stuck,

Thanks in advance,
Chris
58c44a4a506d878f9a112f1d7b7cb87e?d=identicon&s=25 Jeremy Evans (Guest)
on 2006-03-01 19:50
(Received via mailing list)
On 3/1/06, Chris <evilgeenius@gmail.com> wrote:
>   puts timesheet.employee.name
>   puts timesheet.employee.division.name
>   puts timesheet.employee.location.name
> end
>
> Adding the ":include=>:employee" to the find preloads all the employees
> BUT it does not preload the division and location.  This query returns
> over 1000 timesheets, so it takes a long time to load each division +
> location.
>
> Any ideas how to preload the Division and Location??

Try Edge Rails and the patch at
http://dev.rubyonrails.org/ticket/3913.  If that doesn't work you'll
probably need to use find_by_sql to get the records manually and
modify your code so it is like:

  puts timesheet.date
  puts timesheet.employee_name
  puts timesheet.employee_division_name
  puts timesheet.employee_location_name
This topic is locked and can not be replied to.