Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#1696

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.

Reported by Murray Steele · January 5th, 2009 @ 01:27 PM

State: resolved
Milestone: 2.x
Assigned to: Frederick Cheung Frederick Cheung
Importance: none

Activity

  1. Pratik
    Pratik
    • Assigned user changed from Michael Koziarski to Frederick Cheung

    January 5th, 2009 @ 01:52 PM

  2. Jonathan Lim
    Jonathan Lim

    +1 Code looks like it will work with this strange edge case. Have included a patch to remove unnecessary delete in Murray's patch.

    January 15th, 2009 @ 12:37 PM

  3. Michael Koziarski
    Michael Koziarski

    Any thoughts on this Frederick?

    January 15th, 2009 @ 09:03 PM

  4. Frederick Cheung
    Frederick Cheung

    Don't have time to give this a detailed work over but looks sensible to me

    January 15th, 2009 @ 11:35 PM

  5. Michael Koziarski
    Michael Koziarski

    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?

    January 16th, 2009 @ 12:31 AM

  6. Murray Steele
  7. Repository
    Repository
    • State changed from new to resolved

    (from [db26ace030f6704da6fc80bcc6cd00a2aee664ce]) Ensure NoMethodError isn't raised when some of the nested eager loaded associations are empty [#1696 Problem with nested include missing data and old-style single query eager loa... state:resolved]

    Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...

    March 12th, 2009 @ 02:36 PM