From 1d5854826b27e5e8bfb041c57a49d1e46178b49e Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Wed, 7 Oct 2009 11:57:59 -0300 Subject: Allow preload and eager_load to work on relations at the same time. --- activerecord/lib/active_record/relation.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 24fd29c7f3..db1c9c24de 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -26,9 +26,9 @@ module ActiveRecord end def to_a - if @eager_load_associations.any? - records = catch :invalid_query do - @klass.send(:find_with_associations, { + records = if @eager_load_associations.any? + catch :invalid_query do + return @klass.send(:find_with_associations, { :select => @relation.send(:select_clauses).join(', '), :joins => @relation.joins(relation), :group => @relation.send(:group_clauses).join(', '), @@ -38,12 +38,14 @@ module ActiveRecord }, 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 + @klass.find_by_sql(@relation.to_sql) end + @klass.send(:preload_associations, records, @associations_to_preload) unless @associations_to_preload.empty? + records.each { |record| record.readonly! } if @readonly + records end -- cgit v1.2.3