This project is archived and is in readonly mode.
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 => 'categories.name'
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
-
Pratik January 5th, 2009 @ 01:52 PM
- Assigned user changed from Michael Koziarski to Frederick Cheung
-
Jonathan Lim January 15th, 2009 @ 12:37 PM
+1 Code looks like it will work with this strange edge case. Have included a patch to remove unnecessary delete in Murray's patch.
-
Frederick Cheung January 15th, 2009 @ 11:35 PM
Don't have time to give this a detailed work over but looks sensible to me
-
Michael Koziarski January 16th, 2009 @ 12:31 AM
OK, the commit message should get redone to just describe the bug getting fixed rather than having review notes. There's some whitespace stuff
Beyond that I think we're good to go. Got time to wrap it up murray?
-
Repository March 12th, 2009 @ 02:36 PM
- State changed from new to resolved
(from [db26ace030f6704da6fc80bcc6cd00a2aee664ce]) Ensure NoMethodError isn't raised when some of the nested eager loaded associations are empty [#1696 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...
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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 1696 Problem with nested include missing data and old-style single query eager loading (from [db26ace030f6704da6fc80bcc6cd00a2aee664ce]) Ensure ...