public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Association#first and last should not load the association if not needed. [#1091 
state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
DefV (author)
Mon Sep 22 10:14:42 -0700 2008
lifo (committer)
Mon Sep 22 10:22:30 -0700 2008
commit  050e58441bf7e60d167f6708072f8fa7aee2ce76
tree    ab71b73bc91801cc2daa22ba92fb2da5ea1eec99
parent  46939a9b5a0098fddeac99a8a4331f66bdd0710e
...
63
64
65
66
 
67
68
69
...
73
74
75
76
 
77
78
79
...
420
421
422
423
 
 
424
425
426
...
63
64
65
 
66
67
68
69
...
73
74
75
 
76
77
78
79
...
420
421
422
 
423
424
425
426
427
0
@@ -63,7 +63,7 @@ module ActiveRecord
0
       
0
       # Fetches the first one using SQL if possible.
0
       def first(*args)
0
-        if fetch_first_or_last_using_find? args
0
+        if fetch_first_or_last_using_find?(args)
0
           find(:first, *args)
0
         else
0
           load_target unless loaded?
0
@@ -73,7 +73,7 @@ module ActiveRecord
0
 
0
       # Fetches the last one using SQL if possible.
0
       def last(*args)
0
-        if fetch_first_or_last_using_find? args
0
+        if fetch_first_or_last_using_find?(args)
0
           find(:last, *args)
0
         else
0
           load_target unless loaded?
0
@@ -420,7 +420,8 @@ module ActiveRecord
0
         end
0
 
0
         def fetch_first_or_last_using_find?(args)
0
-          args.first.kind_of?(Hash) || !(loaded? || @owner.new_record? || @reflection.options[:finder_sql] || !@target.blank? || args.first.kind_of?(Integer))
0
+          args.first.kind_of?(Hash) || !(loaded? || @owner.new_record? || @reflection.options[:finder_sql] ||
0
+                                         @target.any? { |record| record.new_record? } || args.first.kind_of?(Integer))
0
         end
0
     end
0
   end
...
253
254
255
256
 
257
258
259
...
253
254
255
 
256
257
258
259
0
@@ -253,7 +253,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
0
     assert !devel.projects.loaded?
0
 
0
     assert_equal devel.projects.last, proj
0
-    assert devel.projects.loaded?
0
+    assert !devel.projects.loaded?
0
 
0
     assert !proj.new_record?
0
     assert_equal Developer.find(1).projects.sort_by(&:id).last, proj  # prove join table is updated
...
1009
1010
1011
 
 
 
 
 
 
 
 
 
 
 
 
 
1012
1013
1014
...
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
0
@@ -1009,6 +1009,19 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
0
     assert firm.clients.loaded?
0
   end
0
 
0
+  def test_calling_first_or_last_on_existing_record_with_create_should_not_load_association
0
+    firm = companies(:first_firm)
0
+    firm.clients.create(:name => 'Foo')
0
+    assert !firm.clients.loaded?
0
+
0
+    assert_queries 2 do
0
+      firm.clients.first
0
+      firm.clients.last
0
+    end
0
+
0
+    assert !firm.clients.loaded?
0
+  end
0
+
0
   def test_calling_first_or_last_on_new_record_should_not_run_queries
0
     firm = Firm.new
0
 

Comments