<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -63,7 +63,7 @@ module ActiveRecord
       
       # Fetches the first one using SQL if possible.
       def first(*args)
-        if fetch_first_or_last_using_find? args
+        if fetch_first_or_last_using_find?(args)
           find(:first, *args)
         else
           load_target unless loaded?
@@ -73,7 +73,7 @@ module ActiveRecord
 
       # Fetches the last one using SQL if possible.
       def last(*args)
-        if fetch_first_or_last_using_find? args
+        if fetch_first_or_last_using_find?(args)
           find(:last, *args)
         else
           load_target unless loaded?
@@ -420,7 +420,8 @@ module ActiveRecord
         end
 
         def fetch_first_or_last_using_find?(args)
-          args.first.kind_of?(Hash) || !(loaded? || @owner.new_record? || @reflection.options[:finder_sql] || !@target.blank? || args.first.kind_of?(Integer))
+          args.first.kind_of?(Hash) || !(loaded? || @owner.new_record? || @reflection.options[:finder_sql] ||
+                                         @target.any? { |record| record.new_record? } || args.first.kind_of?(Integer))
         end
     end
   end</diff>
      <filename>activerecord/lib/active_record/associations/association_collection.rb</filename>
    </modified>
    <modified>
      <diff>@@ -253,7 +253,7 @@ class HasAndBelongsToManyAssociationsTest &lt; ActiveRecord::TestCase
     assert !devel.projects.loaded?
 
     assert_equal devel.projects.last, proj
-    assert devel.projects.loaded?
+    assert !devel.projects.loaded?
 
     assert !proj.new_record?
     assert_equal Developer.find(1).projects.sort_by(&amp;:id).last, proj  # prove join table is updated</diff>
      <filename>activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1009,6 +1009,19 @@ class HasManyAssociationsTest &lt; ActiveRecord::TestCase
     assert firm.clients.loaded?
   end
 
+  def test_calling_first_or_last_on_existing_record_with_create_should_not_load_association
+    firm = companies(:first_firm)
+    firm.clients.create(:name =&gt; 'Foo')
+    assert !firm.clients.loaded?
+
+    assert_queries 2 do
+      firm.clients.first
+      firm.clients.last
+    end
+
+    assert !firm.clients.loaded?
+  end
+
   def test_calling_first_or_last_on_new_record_should_not_run_queries
     firm = Firm.new
 </diff>
      <filename>activerecord/test/cases/associations/has_many_associations_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>46939a9b5a0098fddeac99a8a4331f66bdd0710e</id>
    </parent>
  </parents>
  <author>
    <name>Jan De Poorter</name>
    <login>DefV</login>
    <email>jan@defv.be</email>
  </author>
  <url>http://github.com/rails/rails/commit/050e58441bf7e60d167f6708072f8fa7aee2ce76</url>
  <id>050e58441bf7e60d167f6708072f8fa7aee2ce76</id>
  <committed-date>2008-09-22T10:22:30-07:00</committed-date>
  <authored-date>2008-09-22T10:14:42-07:00</authored-date>
  <message>Association#first and last should not load the association if not needed. [#1091 state:resolved]

Signed-off-by: Pratik Naik &lt;pratiknaik@gmail.com&gt;</message>
  <tree>ab71b73bc91801cc2daa22ba92fb2da5ea1eec99</tree>
  <committer>
    <name>Pratik Naik</name>
    <login>lifo</login>
    <email>pratiknaik@gmail.com</email>
  </committer>
</commit>
