Skip to content

Commit

Permalink
remove base class from belongs_to polymorphic association
Browse files Browse the repository at this point in the history
  • Loading branch information
dilpreet92 committed Aug 26, 2015
1 parent 479217c commit cbb5064
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
23 changes: 23 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,26 @@
* Fix polymorphic true with inheritance.

Example:

class Asset < ActiveRecord::Base
belongs_to :attachable, polymorphic: true
end

class Post < ActiveRecord::Base
# Post doesn't have a type column
has_many :assets, as: :attachable, dependent: :destroy
end
class GuestPost < Post
end
class MemberPost < Post
end

Now attachable_type can be Post, GuestPost or MemberPost

*Dilpreet Singh*

* Only try to nullify has_one target association if the record is persisted.

Fixes #21223.
Expand Down
Expand Up @@ -11,7 +11,7 @@ def klass

def replace_keys(record)
super
owner[reflection.foreign_type] = record.class.base_class.name
owner[reflection.foreign_type] = record.class.name
end

def remove_keys
Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/cases/associations/join_model_test.rb
Expand Up @@ -117,12 +117,12 @@ def test_polymorphic_has_many_create_model_with_inheritance
assert_instance_of SpecialPost, post

tagging = tags(:misc).taggings.create(:taggable => post)
assert_equal "Post", tagging.taggable_type
assert_equal "SpecialPost", tagging.taggable_type
end

def test_polymorphic_has_one_create_model_with_inheritance
tagging = tags(:misc).create_tagging(:taggable => posts(:thinking))
assert_equal "Post", tagging.taggable_type
assert_equal "SpecialPost", tagging.taggable_type
end

def test_set_polymorphic_has_many
Expand Down

0 comments on commit cbb5064

Please sign in to comment.