public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rubyruy/rails.git
Ensure that save on child object fails for invalid belongs_to
association. TAKE TWO! - now with more unit tests! (which pass anyway for some 
reason!)
Ruy Asan (author)
Thu Jun 05 14:30:27 -0700 2008
commit  f4cd52798b2cc1e4bf952b38712180e64cc91d51
tree    1736c8d2f9ca6db027081b0ee7a39366830c20e3
parent  ed0cb91a830f317e3a8192a90294c1005f6156c2
...
936
937
938
939
 
 
 
940
941
942
...
936
937
938
 
939
940
941
942
943
944
0
@@ -936,7 +936,9 @@ module ActiveRecord
0
             "#{reflection.class_name}.send(:attr_readonly,\"#{cache_column}\".intern) if defined?(#{reflection.class_name}) && #{reflection.class_name}.respond_to?(:attr_readonly)"
0
           )
0
         end
0
-
0
+        
0
+        add_single_associated_save_callbacks(reflection.name) 
0
+        
0
         configure_dependency_for_belongs_to(reflection)
0
       end
0
 
...
1
2
3
 
4
5
6
...
383
384
385
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
386
387
388
...
1
2
3
4
5
6
7
...
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
0
@@ -1,6 +1,7 @@
0
 require "cases/helper"
0
 require 'models/developer'
0
 require 'models/project'
0
+require 'models/circular_belonging'
0
 require 'models/company'
0
 require 'models/topic'
0
 require 'models/reply'
0
@@ -383,6 +384,31 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
0
     assert_raise(ActiveRecord::ReadOnlyRecord) { companies(:first_client).readonly_firm.save! }
0
     assert companies(:first_client).readonly_firm.readonly?
0
   end
0
+
0
+  def test_save_fails_for_invalid_belongs_to
0
+    assert log = AuditLog.create(:developer_id=>0,:message=>"")
0
+    
0
+    log.developer = Developer.new
0
+    
0
+    assert !log.developer.valid?
0
+    assert !log.valid?
0
+    assert !log.save
0
+    assert_equal "is invalid", log.errors.on("developer")
0
+  end
0
+  
0
+  # This is supposed to test against the problem described here:
0
+  # http://groups.google.com/group/rubyonrails-core/browse_thread/thread/652d0d7e6d455c08
0
+  # (which reverted this patch the first time)
0
+  # However, I cannot reproduce the problem. Perhaps changes in the core
0
+  # have made it go away?
0
+  def test_circular_save_for_belongs_to
0
+    assert romeo = Romeo.create(:montague=>true)
0
+    romeo.juliet = Juliet.new
0
+    
0
+    assert !romeo.valid?
0
+    assert !romeo.save
0
+    assert_equal "is invalid", romeo.errors.on("juliet")
0
+  end
0
   
0
   def test_polymorphic_assignment_foreign_type_field_updating
0
     # should update when assigning a saved record
...
166
167
168
 
 
 
 
169
170
171
...
321
322
323
 
 
 
 
324
325
326
...
166
167
168
169
170
171
172
173
174
175
...
325
326
327
328
329
330
331
332
333
334
0
@@ -166,6 +166,10 @@ ActiveRecord::Schema.define do
0
   create_table :jobs, :force => true do |t|
0
     t.integer :ideal_reference_id
0
   end
0
+  
0
+  create_table :juliets, :force => true do |t|
0
+    t.integer :romeo_id
0
+  end
0
 
0
   create_table :keyboards, :force => true, :id  => false do |t|
0
     t.primary_key :key_number
0
@@ -321,6 +325,10 @@ ActiveRecord::Schema.define do
0
     t.integer :post_id, :null => false
0
     t.integer :person_id, :null => false
0
   end
0
+  
0
+  create_table :romeos, :force => true do |t|
0
+    t.integer :juliet_id
0
+  end
0
 
0
   create_table :shape_expressions, :force => true do |t|
0
     t.string  :paint_type

Comments