public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
belongs_to polymorphic association assignments update the foreign_id and 
foreign_type fields regardless of whether the record being assigned is new or 
not.

fixes the following scenarios:
* I have validates_inclusion_of on the type field for a polymorphic belongs_to 
association.  I assign a new record to the model's polymorphic relationship of 
the proper type.  validation fails because the type field has not been updated.


* I replace the value for a ppolymorphic association to a new record of another 
class.  The type field still says its the previous class, and the id field 
points to the previous record as well.

[#191 state:closed]
Tim Harper (author)
Tue May 13 18:17:40 -0700 2008
technoweenie (committer)
Sat May 31 13:27:25 -0700 2008
commit  0580b31b36c0f7dd1a0f8bdd1b1806e3bd65b22d
tree    8bb596cb3279cf0b703edd6e8a3a70a7b443a082
parent  f7015336f66d284cff8ecb89df9f430791ac57ea
...
7
8
9
10
11
12
13
 
 
14
15
16
...
7
8
9
 
 
 
 
10
11
12
13
14
0
@@ -7,10 +7,8 @@ module ActiveRecord
0
         else
0
           @target = (AssociationProxy === record ? record.target : record)
0
 
0
-          unless record.new_record?
0
-            @owner[@reflection.primary_key_name] = record.id
0
-            @owner[@reflection.options[:foreign_type]] = record.class.base_class.name.to_s
0
-          end
0
+          @owner[@reflection.primary_key_name] = record.id
0
+          @owner[@reflection.options[:foreign_type]] = record.class.base_class.name.to_s
0
 
0
           @updated = true
0
         end
...
12
13
14
 
 
15
16
17
...
381
382
383
384
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385
...
12
13
14
15
16
17
18
19
...
383
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
0
@@ -12,6 +12,8 @@ require 'models/author'
0
 require 'models/tag'
0
 require 'models/tagging'
0
 require 'models/comment'
0
+require 'models/sponsor'
0
+require 'models/member'
0
 
0
 class BelongsToAssociationsTest < ActiveRecord::TestCase
0
   fixtures :accounts, :companies, :developers, :projects, :topics,
0
@@ -381,5 +383,30 @@ 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
+  
0
+  def test_polymorphic_assignment_foreign_type_field_updating
0
+    # should update when assigning a saved record
0
+    sponsor = Sponsor.new
0
+    member = Member.create
0
+    sponsor.sponsorable = member
0
+    assert_equal "Member", sponsor.sponsorable_type
0
+    
0
+    # should update when assigning a new record
0
+    sponsor = Sponsor.new
0
+    member = Member.new
0
+    sponsor.sponsorable = member
0
+    assert_equal "Member", sponsor.sponsorable_type
0
+  end
0
+  
0
+  def test_polymorphic_assignment_updates_foreign_id_field_for_new_and_saved_records
0
+    sponsor = Sponsor.new
0
+    saved_member = Member.create
0
+    new_member = Member.new
0
+    
0
+    sponsor.sponsorable = saved_member
0
+    assert_equal saved_member.id, sponsor.sponsorable_id
0
+    
0
+    sponsor.sponsorable = new_member
0
+    assert_equal nil, sponsor.sponsorable_id
0
+  end
0
 end

Comments