Permalink
Browse files

Association preloading no longer stops if it hits a nil object [#1630

…state:resolved]

Signed-off-by: Frederick Cheung <frederick.cheung@gmail.com>
  • Loading branch information...
1 parent c9d4335 commit eb457ceee13779ade67e1bdebd2919d476148277 Pivotal Labs committed with fcheung Dec 24, 2008
@@ -94,8 +94,8 @@ def preload_associations(records, associations, preload_options={})
raise "parent must be an association name" unless parent.is_a?(String) || parent.is_a?(Symbol)
preload_associations(records, parent, preload_options)
reflection = reflections[parent]
- parents = records.map {|record| record.send(reflection.name)}.flatten
- unless parents.empty? || parents.first.nil?
+ parents = records.map {|record| record.send(reflection.name)}.flatten.compact
+ unless parents.empty?
parents.first.class.preload_associations(parents, child)
end
end
@@ -104,6 +104,14 @@ def test_eager_association_loading_of_stis_with_multiple_references
authors.first.posts.first.special_comments.first.post.very_special_comment
end
end
+
+ def test_eager_association_loading_where_first_level_returns_nil
+ authors = Author.find(:all, :include => {:post_about_thinking => :comments}, :order => 'authors.id DESC')
+ assert_equal [authors(:mary), authors(:david)], authors
+ assert_no_queries do
+ authors[1].post_about_thinking.comments.first
+ end
+ end
end
require 'models/vertex'

0 comments on commit eb457ce

Please sign in to comment.