Skip to content
This repository
Browse code

Handle polymorphic_type NOT NULL-able columns as well.

  • Loading branch information...
commit 1c614bcb0fb65f0a5541263deacda498e52f447b 1 parent 1a959ad
authored May 21, 2011
2  activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
@@ -19,7 +19,7 @@ def inverse_reflection_for(record)
19 19
 
20 20
         def klass
21 21
           type = owner[reflection.foreign_type]
22  
-          type && type.constantize
  22
+          type.presence && type.constantize
23 23
         end
24 24
 
25 25
         def raise_on_type_mismatch(record)
11  activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -158,6 +158,17 @@ def test_with_condition
158 158
     assert_not_nil Company.find(3).firm_with_condition, "Microsoft should have a firm"
159 159
   end
160 160
 
  161
+  def test_polymorphic_association_class
  162
+    sponsor = Sponsor.new
  163
+    assert_nil sponsor.association(:sponsorable).send(:klass)
  164
+
  165
+    sponsor.sponsorable_type = '' # the column doesn't have to be declared NOT NULL
  166
+    assert_nil sponsor.association(:sponsorable).send(:klass)
  167
+
  168
+    sponsor.sponsorable = Member.new :name => "Bert"
  169
+    assert_equal Member, sponsor.association(:sponsorable).send(:klass)
  170
+  end
  171
+
161 172
   def test_with_polymorphic_and_condition
162 173
     sponsor = Sponsor.create
163 174
     member = Member.create :name => "Bert"

0 notes on commit 1c614bc

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