Skip to content

Commit

Permalink
Ensure NoMethodError isn't raised when some of the nested eager loade…
Browse files Browse the repository at this point in the history
…d associations are empty [#1696 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
h-lame authored and lifo committed Mar 12, 2009
1 parent 92dadf6 commit db26ace
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
13 changes: 9 additions & 4 deletions activerecord/lib/active_record/associations.rb
Expand Up @@ -1856,17 +1856,22 @@ def build_join_association(reflection, parent)
def construct(parent, associations, joins, row)
case associations
when Symbol, String
while (join = joins.shift).reflection.name.to_s != associations.to_s
raise ConfigurationError, "Not Enough Associations" if joins.empty?
end
join = joins.detect{|j| j.reflection.name.to_s == associations.to_s && j.parent_table_name == parent.class.table_name }
raise(ConfigurationError, "No such association") if join.nil?

joins.delete(join)
construct_association(parent, join, row)
when Array
associations.each do |association|
construct(parent, association, joins, row)
end
when Hash
associations.keys.sort{|a,b|a.to_s<=>b.to_s}.each do |name|
association = construct_association(parent, joins.shift, row)
join = joins.detect{|j| j.reflection.name.to_s == name.to_s && j.parent_table_name == parent.class.table_name }
raise(ConfigurationError, "No such association") if join.nil?

association = construct_association(parent, join, row)
joins.delete(join)
construct(association, associations[name], joins, row) if association
end
else
Expand Down
@@ -1,4 +1,9 @@
require 'cases/helper'
require 'models/author'
require 'models/post'
require 'models/comment'
require 'models/category'
require 'models/categorization'

module Remembered
def self.included(base)
Expand Down Expand Up @@ -99,3 +104,27 @@ def test_include_query
end
end
end

class EagerLoadNestedIncludeWithMissingDataTest < ActiveRecord::TestCase
def setup
@davey_mcdave = Author.create(:name => 'Davey McDave')
@first_post = @davey_mcdave.posts.create(:title => 'Davey Speaks', :body => 'Expressive wordage')
@first_comment = @first_post.comments.create(:body => 'Inflamatory doublespeak')
@first_categorization = @davey_mcdave.categorizations.create(:category => Category.first, :post => @first_post)
end

def teardown
@davey_mcdave.destroy
@first_post.destroy
@first_comment.destroy
@first_categorization.destroy
end

def test_missing_data_in_a_nested_include_should_not_cause_errors_when_constructing_objects
assert_nothing_raised do
# @davey_mcdave doesn't have any author_favorites
includes = {:posts => :comments, :categorizations => :category, :author_favorites => :favorite_author }
Author.all :include => includes, :conditions => {:authors => {:name => @davey_mcdave.name}}, :order => 'categories.name'
end
end
end

0 comments on commit db26ace

Please sign in to comment.