This project is archived and is in readonly mode.

#1696 ✓resolved
Murray Steele

Problem with nested include missing data and old-style single query eager loading

Reported by Murray Steele | January 5th, 2009 @ 01:27 PM | in 2.x

If we write a find as follows (using the AR test schema):

Author.find :all, :include => {:posts => :comments, :categorizations => :category, :author_favorites => :favorite_author }, :order => ''

And the data is such that there is an author that has posts with comments and categorizations with categories but no author_favorites we'll get a NoMethodError as ActiveRecord tries to call comments= on a Categorization instance. If there were only 2 rather than 3 includes there'd be no error (possibly an extra SQL query) and if the include with the missing data isn't the 1st when the joins are alphabetised, again there would be no error.

The problem occurs because JoinDependency uses 2 arrays (one of associations and one of joins) to interrogate rows of data and construct the correct objects and their associations. The code assumes that the 2 arrays never get out of sync, but in this case they do. It's only a problem when there are 2 joins after the join with missing data, otherwise the arrays exhaust themselves before the error can happen.

The supplied patch changes JoinDependency to find the correct join from the array of joins based on the association we are trying to build, rather than just assume the arrays are synced up.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

Referenced by