diff options
author | Emilio Tagua <miloops@gmail.com> | 2009-09-01 15:36:09 -0300 |
---|---|---|
committer | Emilio Tagua <miloops@gmail.com> | 2009-09-01 15:36:09 -0300 |
commit | c01c21b31d590f7e8d12e3ae083fcdf0f0c6fd54 (patch) | |
tree | 01d91af9e871be42012621af2f51107c0333b924 /activerecord/lib | |
parent | 6b67df70ab1bc42d9a05571144cdf5614a7d4a6a (diff) | |
download | rails-c01c21b31d590f7e8d12e3ae083fcdf0f0c6fd54.tar.gz rails-c01c21b31d590f7e8d12e3ae083fcdf0f0c6fd54.tar.bz2 rails-c01c21b31d590f7e8d12e3ae083fcdf0f0c6fd54.zip |
Added association preload to relation.
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 23 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 8 |
2 files changed, 28 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 402d68c36e..1c12189e15 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -664,11 +664,28 @@ 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) + 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? + 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 diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 4b53857d36..6abb2df8ff 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -6,6 +6,12 @@ module ActiveRecord def initialize(klass, relation) @klass, @relation = klass, relation @readonly = false + @associations_to_preload = [] + end + + def preload(association) + @associations_to_preload << association + @associations_to_preload.flatten! end def readonly @@ -16,6 +22,8 @@ module ActiveRecord 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 records |