aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb4
-rwxr-xr-xactiverecord/lib/active_record/base.rb19
-rw-r--r--activerecord/lib/active_record/relation.rb32
3 files changed, 46 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 1e1f1a4c57..0888c41396 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1458,9 +1458,9 @@ module ActiveRecord
after_destroy(method_name)
end
- def find_with_associations(options = {})
+ def find_with_associations(options = {}, join_dependency = nil)
catch :invalid_query do
- join_dependency = JoinDependency.new(self, merge_includes(scope(:find, :include), options[:include]), options[:joins])
+ join_dependency ||= JoinDependency.new(self, merge_includes(scope(:find, :include), options[:include]), options[:joins])
rows = select_all_rows(options, join_dependency)
return join_dependency.instantiate(rows)
end
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index a1b6606e3e..668506c01a 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -664,11 +664,23 @@ module ActiveRecord #:nodoc:
# This is an alias for find(:all). You can pass in all the same arguments to this method as you can
# to find(:all)
def all(*args)
- if args.empty? && !scoped?(:find)
- arel_table
+ options = args.extract_options!
+
+ if options.empty? && !scoped?(:find)
+ relation = arel_table
else
- construct_finder_arel(*args)
+ relation = construct_finder_arel(options)
+ include_associations = merge_includes(scope(:find, :include), options[:include])
+
+ if include_associations.any?
+ if references_eager_loaded_tables?(options)
+ relation.eager_load(include_associations)
+ else
+ relation.preload(include_associations)
+ end
+ end
end
+ relation
end
# Executes a custom SQL query against your database and returns all the results. The results will
@@ -1719,7 +1731,6 @@ module ActiveRecord #:nodoc:
relation = relation.readonly if options[:readonly]
relation
-
end
def construct_finder_sql(options, scope = scope(:find))
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 4b53857d36..24fd29c7f3 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -6,6 +6,18 @@ module ActiveRecord
def initialize(klass, relation)
@klass, @relation = klass, relation
@readonly = false
+ @associations_to_preload = []
+ @eager_load_associations = []
+ end
+
+ def preload(association)
+ @associations_to_preload += association
+ self
+ end
+
+ def eager_load(association)
+ @eager_load_associations += association
+ self
end
def readonly
@@ -14,9 +26,23 @@ module ActiveRecord
end
def to_a
- records = @klass.find_by_sql(@relation.to_sql)
-
- records.each { |record| record.readonly! } if @readonly
+ if @eager_load_associations.any?
+ records = catch :invalid_query do
+ @klass.send(:find_with_associations, {
+ :select => @relation.send(:select_clauses).join(', '),
+ :joins => @relation.joins(relation),
+ :group => @relation.send(:group_clauses).join(', '),
+ :order => @relation.send(:order_clauses).join(', '),
+ :conditions => @relation.send(:where_clauses).join("\n\tAND "),
+ :limit => @relation.taken
+ },
+ ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, @eager_load_associations, nil))
+ end
+ else
+ records = @klass.find_by_sql(@relation.to_sql)
+ @klass.send(:preload_associations, records, @associations_to_preload) unless @associations_to_preload.empty?
+ records.each { |record| record.readonly! } if @readonly
+ end
records
end