mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Merge pull request #1187 from thedarkone/polymorphic-class-fix
Handle polymorphic_type NOT NULL columns
This commit is contained in:
@@ -19,7 +19,7 @@ module ActiveRecord
|
||||
|
||||
def klass
|
||||
type = owner[reflection.foreign_type]
|
||||
type && type.constantize
|
||||
type.presence && type.constantize
|
||||
end
|
||||
|
||||
def raise_on_type_mismatch(record)
|
||||
|
||||
@@ -158,6 +158,17 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
|
||||
assert_not_nil Company.find(3).firm_with_condition, "Microsoft should have a firm"
|
||||
end
|
||||
|
||||
def test_polymorphic_association_class
|
||||
sponsor = Sponsor.new
|
||||
assert_nil sponsor.association(:sponsorable).send(:klass)
|
||||
|
||||
sponsor.sponsorable_type = '' # the column doesn't have to be declared NOT NULL
|
||||
assert_nil sponsor.association(:sponsorable).send(:klass)
|
||||
|
||||
sponsor.sponsorable = Member.new :name => "Bert"
|
||||
assert_equal Member, sponsor.association(:sponsorable).send(:klass)
|
||||
end
|
||||
|
||||
def test_with_polymorphic_and_condition
|
||||
sponsor = Sponsor.create
|
||||
member = Member.create :name => "Bert"
|
||||
|
||||
Reference in New Issue
Block a user