Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove base class from belongs_to polymorphic association #21385

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,28 @@
## Rails 6.0.0.alpha (Unreleased) ##

* 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*

* Fix `#columns_for_distinct` of MySQL and PostgreSQL to make
`ActiveRecord::FinderMethods#limited_ids_for` use correct primary key values
even if `ORDER BY` columns include other table's primary key.
Expand Down
Expand Up @@ -13,7 +13,7 @@ def klass

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

def different_target?(record)
Expand Down
8 changes: 4 additions & 4 deletions activerecord/test/cases/associations/join_model_test.rb
Expand Up @@ -118,13 +118,13 @@ def test_polymorphic_has_many_create_model_with_inheritance
post = posts(:thinking)
assert_instance_of SpecialPost, post

tagging = tags(:misc).taggings.create(taggable: post)
assert_equal "Post", tagging.taggable_type
tagging = tags(:misc).taggings.create(:taggable => post)
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
tagging = tags(:misc).create_tagging(:taggable => posts(:thinking))
assert_equal "SpecialPost", tagging.taggable_type
end

def test_set_polymorphic_has_many
Expand Down