public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Ensure hm:t preloading honours reflection options. [#137 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
fcheung (author)
Wed May 07 13:35:41 -0700 2008
lifo (committer)
Sun May 11 12:01:14 -0700 2008
commit  3f0dccbbc7c98938349650033ff9a41a814d300d
tree    c19906d9c79d3023c7769103584dd8572c5e5870
parent  8f2f88f128104355b100487008800aeb369ca425
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Ensure hm:t preloading honours reflection options. Resolves #137. [Frederick Cheung]
0
+
0
 * Added protection against duplicate migration names (Aslak Hellesøy) [#112]
0
 
0
 * Base#instantiate_time_object: eliminate check for Time.zone, since we can assume this is set if time_zone_aware_attributes is set to true [Geoff Buesing]
...
143
144
145
146
 
 
147
148
149
...
251
252
253
254
255
 
 
256
257
258
259
 
 
260
261
262
...
143
144
145
 
146
147
148
149
150
...
252
253
254
 
 
255
256
257
258
 
 
259
260
261
262
263
0
@@ -143,7 +143,8 @@ module ActiveRecord
0
           through_primary_key = through_reflection.primary_key_name
0
           unless through_records.empty?
0
             source = reflection.source_reflection.name
0
-            through_records.first.class.preload_associations(through_records, source)
0
+            #add conditions from reflection!
0
+            through_records.first.class.preload_associations(through_records, source, reflection.options)
0
             through_records.each do |through_record|
0
               add_preloaded_records_to_collection(id_to_record_map[through_record[through_primary_key].to_s],
0
                                                  reflection.name, through_record.send(source))
0
@@ -251,12 +252,12 @@ module ActiveRecord
0
         conditions << append_conditions(options, preload_options)
0
 
0
         reflection.klass.find(:all,
0
-                              :select => (options[:select] || "#{table_name}.*"),
0
-                              :include => options[:include],
0
+                              :select => (preload_options[:select] || options[:select] || "#{table_name}.*"),
0
+                              :include => preload_options[:include] || options[:include],
0
                               :conditions => [conditions, ids],
0
                               :joins => options[:joins],
0
-                              :group => options[:group],
0
-                              :order => options[:order])
0
+                              :group => preload_options[:group] || options[:group],
0
+                              :order => preload_options[:order] || options[:order])
0
       end
0
 
0
 
...
275
276
277
 
 
 
 
 
 
 
 
 
 
 
278
279
280
...
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
0
@@ -275,6 +275,17 @@ class EagerAssociationTest < ActiveRecord::TestCase
0
                  Author.find(:first, :order => 'authors.id').hello_post_comments.sort_by(&:id)
0
   end
0
 
0
+  def test_eager_with_has_many_through_join_model_with_conditions_on_top_level
0
+    assert_equal comments(:more_greetings), Author.find(authors(:david).id, :include => :comments_with_order_and_conditions).comments_with_order_and_conditions.first
0
+  end
0
+
0
+  def test_eager_with_has_many_through_join_model_with_include
0
+    author_comments = Author.find(authors(:david).id, :include => :comments_with_include).comments_with_include.to_a
0
+    assert_no_queries do
0
+      author_comments.first.post.title
0
+    end
0
+  end
0
+
0
   def test_eager_with_has_many_and_limit
0
     posts = Post.find(:all, :order => 'posts.id asc', :include => [ :author, :comments ], :limit => 2)
0
     assert_equal 2, posts.size
...
17
18
19
 
 
 
 
20
21
22
...
17
18
19
20
21
22
23
24
25
26
0
@@ -17,6 +17,10 @@ class Author < ActiveRecord::Base
0
   end
0
   has_many :comments, :through => :posts
0
   has_many :comments_containing_the_letter_e, :through => :posts, :source => :comments
0
+  has_many :comments_with_order_and_conditions, :through => :posts, :source => :comments, :order => 'comments.body', :conditions => "comments.body like 'Thank%'"
0
+  has_many :comments_with_include, :through => :posts, :source => :comments, :include => :post
0
+
0
+
0
   has_many :comments_desc, :through => :posts, :source => :comments, :order => 'comments.id DESC'
0
   has_many :limited_comments, :through => :posts, :source => :comments, :limit => 1
0
   has_many :funky_comments, :through => :posts, :source => :comments

Comments