Skip to content
This repository
Browse code

Make reset return nil when using a dangling belongs_to association. C…

…urrent behaviour is to return false which can be confusing. Closes #10293 [fcheung]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8236 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit e302759451fbed36d7e8916d7b5c8377762e3efd 1 parent 0a9bc59
Michael Koziarski authored November 29, 2007
2  activerecord/lib/active_record/associations/association_proxy.rb
@@ -46,8 +46,8 @@ def conditions
46 46
       alias :sql_conditions :conditions
47 47
       
48 48
       def reset
49  
-        @target = nil
50 49
         @loaded = false
  50
+        @target = nil
51 51
       end
52 52
 
53 53
       def reload
17  activerecord/test/associations_test.rb
@@ -122,6 +122,23 @@ def test_save_on_parent_saves_children
122 122
     developer = Developer.create :name => "Bryan", :salary => 50_000
123 123
     assert_equal 1, developer.reload.audit_logs.size
124 124
   end
  125
+
  126
+  def test_failed_reload_returns_nil
  127
+    p = setup_dangling_association
  128
+    assert_nil p.author.reload
  129
+  end
  130
+
  131
+  def test_failed_reset_returns_nil
  132
+    p = setup_dangling_association
  133
+    assert_nil p.author.reset
  134
+  end
  135
+
  136
+  def setup_dangling_association
  137
+    josh = Author.create(:name => "Josh")
  138
+    p = Post.create(:title => "New on Edge", :body => "More cool stuff!", :author => josh)
  139
+    josh.destroy
  140
+    p
  141
+  end
125 142
 end
126 143
 
127 144
 class HasOneAssociationsTest < Test::Unit::TestCase

0 notes on commit e302759

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