Permalink
Browse files

query the association rather than send the method for the association…

… name
  • Loading branch information...
1 parent 169c214 commit 594ce0f0a84f6de7719d59b55eee4b859efd6461 @tenderlove tenderlove committed Aug 27, 2013
@@ -27,17 +27,18 @@ def associated_records_by_owner
def through_records_by_owner
Preloader.new(owners, through_reflection.name, through_scope).run
- Hash[owners.map do |owner|
- through_records = Array.wrap(owner.send(through_reflection.name))
+ owners.each_with_object({}) do |owner, h|
+ association = owner.association through_reflection.name
+ through_records = Array(association.reader)
# Dont cache the association - we would only be caching a subset
if (through_scope != through_reflection.klass.unscoped) ||
(reflection.options[:source_type] && through_reflection.collection?)
- owner.association(through_reflection.name).reset
+ association.reset
end
- [owner, through_records]
- end]
+ h[owner] = through_records
+ end
end
def through_scope
@@ -52,12 +52,10 @@ def test_eager_association_loading_grafts_stashed_associations_to_correct_parent
def test_cascaded_eager_association_loading_with_join_for_count
categories = Category.joins(:categorizations).includes([{:posts=>:comments}, :authors])
- assert_nothing_raised do
- assert_equal 4, categories.count
- assert_equal 4, categories.to_a.count
- assert_equal 3, categories.distinct.count
- assert_equal 3, categories.to_a.uniq.size # Must uniq since instantiating with inner joins will get dupes
- end
+ assert_equal 4, categories.count
+ assert_equal 4, categories.to_a.count
+ assert_equal 3, categories.distinct.count
+ assert_equal 3, categories.to_a.uniq.size # Must uniq since instantiating with inner joins will get dupes
end
def test_cascaded_eager_association_loading_with_duplicated_includes

0 comments on commit 594ce0f

Please sign in to comment.