aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2012-08-01 21:55:24 +0100
committerJon Leighton <j@jonathanleighton.com>2012-08-01 21:59:51 +0100
commit66aaca4e5d69b6acbbfc97f298b775fa13b7335a (patch)
treef150a53e66916e445341259b9de4a477e1d5b9c8 /activerecord/lib/active_record/relation.rb
parent437851ea02983e7f039de1a09a69ee32f7681134 (diff)
downloadrails-66aaca4e5d69b6acbbfc97f298b775fa13b7335a.tar.gz
rails-66aaca4e5d69b6acbbfc97f298b775fa13b7335a.tar.bz2
rails-66aaca4e5d69b6acbbfc97f298b775fa13b7335a.zip
Refactor a bit
This doesn't change the exernal behavior, but it moves some code around to where I think it properly belongs.
Diffstat (limited to 'activerecord/lib/active_record/relation.rb')
-rw-r--r--activerecord/lib/active_record/relation.rb75
1 files changed, 37 insertions, 38 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index a8170dc489..bd1a6be81c 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -186,45 +186,9 @@ module ActiveRecord
# Converts relation objects to Array.
def to_a
- # We monitor here the entire execution rather than individual SELECTs
- # because from the point of view of the user fetching the records of a
- # relation is a single unit of work. You want to know if this call takes
- # too long, not if the individual queries take too long.
- #
- # It could be the case that none of the queries involved surpass the
- # threshold, and at the same time the sum of them all does. The user
- # should get a query plan logged in that case.
- logging_query_plan do
- exec_queries
- end
- end
-
- def exec_queries
- return @records if loaded?
-
- default_scoped = with_default_scope
-
- if default_scoped.equal?(self)
- @records = eager_loading? ? find_with_associations : @klass.find_by_sql(arel, bind_values)
-
- preload = preload_values
- preload += includes_values unless eager_loading?
- preload.each do |associations|
- ActiveRecord::Associations::Preloader.new(@records, associations).run
- end
-
- # @readonly_value is true only if set explicitly. @implicit_readonly is true if there
- # are JOINS and no explicit SELECT.
- readonly = readonly_value.nil? ? @implicit_readonly : readonly_value
- @records.each { |record| record.readonly! } if readonly
- else
- @records = default_scoped.to_a
- end
-
- @loaded = true
+ load
@records
end
- private :exec_queries
def as_json(options = nil) #:nodoc:
to_a.as_json(options)
@@ -473,7 +437,18 @@ module ActiveRecord
#
# Post.where(published: true).load # => #<ActiveRecord::Relation>
def load
- to_a # force reload
+ unless loaded?
+ # We monitor here the entire execution rather than individual SELECTs
+ # because from the point of view of the user fetching the records of a
+ # relation is a single unit of work. You want to know if this call takes
+ # too long, not if the individual queries take too long.
+ #
+ # It could be the case that none of the queries involved surpass the
+ # threshold, and at the same time the sum of them all does. The user
+ # should get a query plan logged in that case.
+ logging_query_plan { exec_queries }
+ end
+
self
end
@@ -576,6 +551,30 @@ module ActiveRecord
private
+ def exec_queries
+ default_scoped = with_default_scope
+
+ if default_scoped.equal?(self)
+ @records = eager_loading? ? find_with_associations : @klass.find_by_sql(arel, bind_values)
+
+ preload = preload_values
+ preload += includes_values unless eager_loading?
+ preload.each do |associations|
+ ActiveRecord::Associations::Preloader.new(@records, associations).run
+ end
+
+ # @readonly_value is true only if set explicitly. @implicit_readonly is true if there
+ # are JOINS and no explicit SELECT.
+ readonly = readonly_value.nil? ? @implicit_readonly : readonly_value
+ @records.each { |record| record.readonly! } if readonly
+ else
+ @records = default_scoped.to_a
+ end
+
+ @loaded = true
+ @records
+ end
+
def references_eager_loaded_tables?
joined_tables = arel.join_sources.map do |join|
if join.is_a?(Arel::Nodes::StringJoin)