From b3d6f77f32072a96e68502b0488bfd6bea1128b4 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 28 Aug 2013 17:44:42 -0700 Subject: only call to_a when we have to --- .../lib/active_record/associations/preloader/association.rb | 6 +++++- .../active_record/associations/preloader/has_and_belongs_to_many.rb | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb index 0cc836f991..59aeb4eb6a 100644 --- a/activerecord/lib/active_record/associations/preloader/association.rb +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -29,6 +29,10 @@ module ActiveRecord end def records_for(ids) + query_scope(ids).to_a + end + + def query_scope(ids) scope.where(association_key.in(ids)) end @@ -77,7 +81,7 @@ module ActiveRecord # Some databases impose a limit on the number of ids in a list (in Oracle it's 1000) # Make several smaller queries if necessary or make one query if the adapter supports it sliced = owner_keys.each_slice(klass.connection.in_clause_length || owner_keys.size) - records = sliced.flat_map { |slice| records_for(slice).to_a } + records = sliced.flat_map { |slice| records_for(slice) } end # Each record may have multiple owners, and vice-versa diff --git a/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb b/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb index 9a3fada380..08a36db877 100644 --- a/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +++ b/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb @@ -12,7 +12,7 @@ module ActiveRecord # Unlike the other associations, we want to get a raw array of rows so that we can # access the aliased column on the join table def records_for(ids) - scope = super + scope = query_scope ids klass.connection.select_all(scope.arel, 'SQL', scope.bind_values) end -- cgit v1.2.3