public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
fix eager loading with dynamic finders
brandon (author)
Mon Jun 09 08:30:48 -0700 2008
technoweenie (committer)
Mon Jun 09 09:05:20 -0700 2008
commit  e94e53f9cd70bee69759661e9771da3fe0ee9554
tree    ebd08651543a0c28ce6ce0ef6d8a0bc9bf7c4430
parent  8bf74c30fe276606214850ae2de76fe0efb08d94
...
1506
1507
1508
1509
 
1510
1511
1512
...
1506
1507
1508
 
1509
1510
1511
1512
0
@@ -1506,7 +1506,7 @@ module ActiveRecord
0
         end
0
 
0
         def order_tables(options)
0
-          order = options[:order]
0
+          order = [options[:order], scope(:find, :order) ].join(", ")
0
           return [] unless order && order.is_a?(String)
0
           order.scan(/([\.\w]+).?\./).flatten
0
         end
...
87
88
89
 
90
91
92
...
87
88
89
90
91
92
93
0
@@ -87,6 +87,7 @@ module ActiveRecord
0
                         :joins => @join_sql,
0
                         :readonly => false,
0
                         :order => @reflection.options[:order],
0
+                        :include => @reflection.options[:include],
0
                         :limit => @reflection.options[:limit] } }
0
         end
0
 
...
100
101
102
103
 
104
105
106
...
100
101
102
 
103
104
105
106
0
@@ -100,7 +100,7 @@ module ActiveRecord
0
           create_scoping = {}
0
           set_belongs_to_association_for(create_scoping)
0
           {
0
-            :find => { :conditions => @finder_sql, :readonly => false, :order => @reflection.options[:order], :limit => @reflection.options[:limit] },
0
+            :find => { :conditions => @finder_sql, :readonly => false, :order => @reflection.options[:order], :limit => @reflection.options[:limit], :include => @reflection.options[:include]},
0
             :create => create_scoping
0
           }
0
         end
...
681
682
683
 
 
 
 
 
 
684
...
681
682
683
684
685
686
687
688
689
690
0
@@ -681,4 +681,10 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
0
     assert_equal developer, project.developers.find(:first)
0
     assert_equal project, developer.projects.find(:first)
0
   end
0
+
0
+  def test_dynamic_find_should_respect_association_include
0
+    # SQL error in sort clause if :include is not included
0
+    # due to Unknown column 'authors.id'
0
+    assert Category.find(1).posts_with_authors_sorted_by_author_id.find_by_title('Welcome to the weblog')
0
+  end
0
 end
...
187
188
189
 
 
 
 
 
 
190
...
187
188
189
190
191
192
193
194
195
196
0
@@ -187,4 +187,10 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
0
     post.people_with_callbacks.clear
0
     assert_equal (%w(Michael David Julian Roger) * 2).sort, log.last(8).collect(&:last).sort
0
   end
0
+
0
+  def test_dynamic_find_should_respect_association_include
0
+    # SQL error in sort clause if :include is not included
0
+    # due to Unknown column 'comments.id'
0
+    assert Person.find(1).posts_with_comments_sorted_by_comment_id.find_by_title('Welcome to the weblog')
0
+  end
0
 end
...
1
2
3
 
4
5
6
...
1
2
3
4
5
6
7
0
@@ -1,6 +1,7 @@
0
 class Author < ActiveRecord::Base
0
   has_many :posts
0
   has_many :posts_with_comments, :include => :comments, :class_name => "Post"
0
+  has_many :posts_with_comments_sorted_by_comment_id, :include => :comments, :class_name => "Post", :order => 'comments.id'
0
   has_many :posts_with_categories, :include => :categories, :class_name => "Post"
0
   has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :order => "posts.id", :class_name => "Post"
0
   has_many :posts_containing_the_letter_a, :class_name => "Post"
...
2
3
4
 
5
6
7
...
2
3
4
5
6
7
8
0
@@ -2,6 +2,7 @@ class Category < ActiveRecord::Base
0
   has_and_belongs_to_many :posts
0
   has_and_belongs_to_many :special_posts, :class_name => "Post"
0
   has_and_belongs_to_many :other_posts, :class_name => "Post"
0
+  has_and_belongs_to_many :posts_with_authors_sorted_by_author_id, :class_name => "Post", :include => :authors, :order => "authors.id"
0
 
0
   has_and_belongs_to_many(:select_testing_posts,
0
                           :class_name => 'Post',
...
6
7
8
9
 
10
...
6
7
8
 
9
10
0
@@ -6,5 +6,5 @@ class Person < ActiveRecord::Base
0
   has_many :references
0
   has_many :jobs, :through => :references
0
   has_one :favourite_reference, :class_name => 'Reference', :conditions => ['favourite=?', true]
0
-
0
+  has_many :posts_with_comments_sorted_by_comment_id, :through => :readers, :source => :post, :include => :comments, :order => 'comments.id'
0
 end

Comments