1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Added eager loading support to Relation and ActiveRecord#all.

This commit is contained in:
Emilio Tagua 2009-10-05 15:24:08 -03:00
parent 3747f896a1
commit 65f055a3ed
4 changed files with 34 additions and 21 deletions

View file

@ -1455,9 +1455,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

View file

@ -666,23 +666,18 @@ module ActiveRecord #:nodoc:
def all(*args)
options = args.extract_options!
if options.empty? && !scoped?(:find)
relation = arel_table
else
relation = construct_finder_arel(options)
include_associations = merge_includes(scope(:find, :include), options[:include])
# if include_associations.any? && references_eager_loaded_tables?(options)
# join_dependency = JoinDependency.new(self, include_associations, options[:joins])
# relation = construct_finder_arel_with_included_associations(options, join_dependency)
# relation.preload(include_associations)
# else
relation = construct_finder_arel(options)
if include_associations.any?
if include_associations.any?
if references_eager_loaded_tables?(options)
relation.eager_load(include_associations)
else
relation.preload(include_associations)
# end
end
end
end
relation

View file

@ -7,11 +7,17 @@ module ActiveRecord
@klass, @relation = klass, relation
@readonly = false
@associations_to_preload = []
@eager_load_associations = []
end
def preload(association)
@associations_to_preload << association
@associations_to_preload.flatten!
@associations_to_preload += association
self
end
def eager_load(association)
@eager_load_associations += association
self
end
def readonly
@ -20,11 +26,23 @@ module ActiveRecord
end
def to_a
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
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

View file

@ -104,7 +104,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
authors.first.posts.first.special_comments.first.post.very_special_comment
end
end
def test_eager_association_loading_where_first_level_returns_nil
authors = Author.find(:all, :include => {:post_about_thinking => :comments}, :order => 'authors.id DESC')
assert_equal [authors(:mary), authors(:david)], authors