Browse files

Fix issue where assigning a empty string to the _type column of a pol…

…ymorphic association throws an error. Now it returns nil for the association as one would expect.

[Fixes #1824]
  • Loading branch information...
1 parent 8467ea3 commit 5a44c701eeea76313806689332d10a4debe923b1 @mikegehard mikegehard committed Jun 23, 2011
View
2 activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
@@ -75,7 +75,7 @@ def record_id(record)
end
def association_class
- @owner[@reflection.options[:foreign_type]] ? @owner[@reflection.options[:foreign_type]].constantize : nil
+ @owner[@reflection.options[:foreign_type]].present? ? @owner[@reflection.options[:foreign_type]].constantize : nil
end
end
end
View
7 activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -161,6 +161,13 @@ def test_with_polymorphic_and_condition
assert_nil sponsor.sponsorable_with_conditions
end
+ def test_polymorphic_with_empty_string_for_type
+ sponsor = Sponsor.create
+ sponsor.sponsorable_type = ""
+
+ assert_nil sponsor.sponsorable
+ end
+
def test_with_select
assert_equal Company.find(2).firm_with_select.attributes.size, 1
assert_equal Company.find(2, :include => :firm_with_select ).firm_with_select.attributes.size, 1

0 comments on commit 5a44c70

Please sign in to comment.