Skip to content
This repository
Browse code

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.
  • Loading branch information...
commit aef47dcf937a5c9f150c50b73cffd9fa9eb64915 1 parent f701533
Tim Harper authored May 13, 2008 technoweenie committed May 31, 2008
6  activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
@@ -7,10 +7,8 @@ def replace(record)
7 7
         else
8 8
           @target = (AssociationProxy === record ? record.target : record)
9 9
 
10  
-          unless record.new_record?
11  
-            @owner[@reflection.primary_key_name] = record.id
12  
-            @owner[@reflection.options[:foreign_type]] = record.class.base_class.name.to_s
13  
-          end
  10
+          @owner[@reflection.primary_key_name] = record.id
  11
+          @owner[@reflection.options[:foreign_type]] = record.class.base_class.name.to_s
14 12
 
15 13
           @updated = true
16 14
         end
29  activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -12,6 +12,8 @@
12 12
 require 'models/tag'
13 13
 require 'models/tagging'
14 14
 require 'models/comment'
  15
+require 'models/sponsor'
  16
+require 'models/member'
15 17
 
16 18
 class BelongsToAssociationsTest < ActiveRecord::TestCase
17 19
   fixtures :accounts, :companies, :developers, :projects, :topics,
@@ -381,5 +383,30 @@ def test_cant_save_readonly_association
381 383
     assert_raise(ActiveRecord::ReadOnlyRecord) { companies(:first_client).readonly_firm.save! }
382 384
     assert companies(:first_client).readonly_firm.readonly?
383 385
   end
384  
-
  386
+  
  387
+  def test_polymorphic_assignment_foreign_type_field_updating
  388
+    # should update when assigning a saved record
  389
+    sponsor = Sponsor.new
  390
+    member = Member.create
  391
+    sponsor.sponsorable = member
  392
+    assert_equal "Member", sponsor.sponsorable_type
  393
+    
  394
+    # should update when assigning a new record
  395
+    sponsor = Sponsor.new
  396
+    member = Member.new
  397
+    sponsor.sponsorable = member
  398
+    assert_equal "Member", sponsor.sponsorable_type
  399
+  end
  400
+  
  401
+  def test_polymorphic_assignment_updates_foreign_id_field_for_new_and_saved_records
  402
+    sponsor = Sponsor.new
  403
+    saved_member = Member.create
  404
+    new_member = Member.new
  405
+    
  406
+    sponsor.sponsorable = saved_member
  407
+    assert_equal saved_member.id, sponsor.sponsorable_id
  408
+    
  409
+    sponsor.sponsorable = new_member
  410
+    assert_equal nil, sponsor.sponsorable_id
  411
+  end
385 412
 end

0 notes on commit aef47dc

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