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 -- Posted via http://www.ruby-forum.com/.
Jeremy Evans
2006-Mar-01 18:49 UTC
[Rails] Eager loading problem. Help greately appreciated
On 3/1/06, Chris <evilgeenius@gmail.com> wrote:> > 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??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