diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-08-27 15:14:48 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-08-27 15:14:48 -0700 |
commit | 594ce0f0a84f6de7719d59b55eee4b859efd6461 (patch) | |
tree | 7bc049bac83ea0edae6f95f6084523744b826c66 /activerecord | |
parent | 169c2141fc4b37788582fe02908f696a56d70a7b (diff) | |
download | rails-594ce0f0a84f6de7719d59b55eee4b859efd6461.tar.gz rails-594ce0f0a84f6de7719d59b55eee4b859efd6461.tar.bz2 rails-594ce0f0a84f6de7719d59b55eee4b859efd6461.zip |
query the association rather than send the method for the association name
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations/preloader/through_association.rb | 11 | ||||
-rw-r--r-- | activerecord/test/cases/associations/cascaded_eager_loading_test.rb | 10 |
2 files changed, 10 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/associations/preloader/through_association.rb b/activerecord/lib/active_record/associations/preloader/through_association.rb index de06931845..b2e8f32625 100644 --- a/activerecord/lib/active_record/associations/preloader/through_association.rb +++ b/activerecord/lib/active_record/associations/preloader/through_association.rb @@ -27,17 +27,18 @@ module ActiveRecord 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 diff --git a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb index e693d34f99..811d91f849 100644 --- a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb +++ b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb @@ -52,12 +52,10 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase 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 |