Skip to content
This repository
Browse code

Added association preload to relation.

  • Loading branch information...
commit c01c21b31d590f7e8d12e3ae083fcdf0f0c6fd54 1 parent 6b67df7
Emilio Tagua authored
23  activerecord/lib/active_record/base.rb
@@ -664,11 +664,28 @@ def last(*args)
664 664
       # This is an alias for find(:all).  You can pass in all the same arguments to this method as you can
665 665
       # to find(:all)
666 666
       def all(*args)
667  
-        if args.empty? && !scoped?(:find)
668  
-          arel_table
  667
+        options = args.extract_options!
  668
+
  669
+
  670
+        if options.empty? #&& !scoped?(:find)
  671
+          relation = arel_table
669 672
         else
670  
-          construct_finder_arel(*args)
  673
+          include_associations = merge_includes(scope(:find, :include), options[:include])
  674
+
  675
+          # if include_associations.any? && references_eager_loaded_tables?(options)
  676
+          #   join_dependency = JoinDependency.new(self, include_associations, options[:joins])
  677
+
  678
+          #   relation = construct_finder_arel_with_included_associations(options, join_dependency)
  679
+
  680
+          #   relation.preload(include_associations)
  681
+          # else
  682
+            relation = construct_finder_arel(options)
  683
+            if include_associations.any?
  684
+              relation.preload(include_associations)
  685
+            # end
  686
+          end
671 687
         end
  688
+        relation
672 689
       end
673 690
 
674 691
       # Executes a custom SQL query against your database and returns all the results.  The results will
8  activerecord/lib/active_record/relation.rb
@@ -6,6 +6,12 @@ class Relation
6 6
     def initialize(klass, relation)
7 7
       @klass, @relation = klass, relation
8 8
       @readonly = false
  9
+      @associations_to_preload = []
  10
+    end
  11
+
  12
+    def preload(association)
  13
+      @associations_to_preload << association
  14
+      @associations_to_preload.flatten!
9 15
     end
10 16
 
11 17
     def readonly
@@ -16,6 +22,8 @@ def readonly
16 22
     def to_a
17 23
       records = @klass.find_by_sql(@relation.to_sql)
18 24
 
  25
+      @klass.send :preload_associations, records, @associations_to_preload unless @associations_to_preload.empty?
  26
+
19 27
       records.each { |record| record.readonly! } if @readonly
20 28
 
21 29
       records
31  activerecord/test/cases/relations_test.rb
... ...
@@ -1,6 +1,7 @@
1 1
 require "cases/helper"
2 2
 require 'models/post'
3 3
 require 'models/topic'
  4
+require 'models/comment'
4 5
 require 'models/reply'
5 6
 require 'models/author'
6 7
 require 'models/entrant'
@@ -8,7 +9,7 @@
8 9
 require 'models/company'
9 10
 
10 11
 class RelationTest < ActiveRecord::TestCase
11  
-  fixtures :authors, :topics, :entrants, :developers, :companies, :developers_projects, :accounts, :categories, :categorizations, :posts
  12
+  fixtures :authors, :topics, :entrants, :developers, :companies, :developers_projects, :accounts, :categories, :categorizations, :posts, :comments
12 13
 
13 14
   def test_finding_with_conditions
14 15
     assert_equal Author.find(:all, :conditions => "name = 'David'"), Author.all.conditions("name = 'David'").to_a
@@ -85,5 +86,33 @@ def test_find_with_readonly_option
85 86
     Developer.all.readonly.each { |d| assert d.readonly? }
86 87
     Developer.all(:readonly => true).each { |d| assert d.readonly? }
87 88
   end
  89
+
  90
+  def test_eager_association_loading_of_stis_with_multiple_references
  91
+    authors = Author.all(:include => { :posts => { :special_comments => { :post => [ :special_comments, :very_special_comment ] } } }, :order => 'comments.body, very_special_comments_posts.body', :conditions => 'posts.id = 4').to_a
  92
+    assert_equal [authors(:david)], authors
  93
+    assert_no_queries do
  94
+      authors.first.posts.first.special_comments.first.post.special_comments
  95
+      authors.first.posts.first.special_comments.first.post.very_special_comment
  96
+    end
  97
+  end
  98
+
  99
+  def test_find_with_included_associations
  100
+    assert_queries(2) do
  101
+      posts = Post.find(:all, :include => :comments)
  102
+      posts.first.comments.first
  103
+    end
  104
+    assert_queries(2) do
  105
+      posts = Post.all(:include => :comments).to_a
  106
+      posts.first.comments.first
  107
+    end
  108
+    assert_queries(2) do
  109
+      posts = Post.find(:all, :include => :author)
  110
+      posts.first.author
  111
+    end
  112
+    assert_queries(2) do
  113
+      posts = Post.all(:include => :author).to_a
  114
+      posts.first.author
  115
+    end
  116
+  end
88 117
 end
89 118
 

0 notes on commit c01c21b

Please sign in to comment.
Something went wrong with that request. Please try again.