aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2013-08-28 17:44:42 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2013-08-28 17:44:42 -0700
commitb3d6f77f32072a96e68502b0488bfd6bea1128b4 (patch)
tree62580b28ad9a7805eea3f7a86d4bcb2856774eca /activerecord/lib/active_record/associations
parent81fb5317dcdec7a9279c1004f3da3204b6c59c76 (diff)
downloadrails-b3d6f77f32072a96e68502b0488bfd6bea1128b4.tar.gz
rails-b3d6f77f32072a96e68502b0488bfd6bea1128b4.tar.bz2
rails-b3d6f77f32072a96e68502b0488bfd6bea1128b4.zip
only call to_a when we have to
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r--activerecord/lib/active_record/associations/preloader/association.rb6
-rw-r--r--activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb2
2 files changed, 6 insertions, 2 deletions
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