Skip to content

Commit 59e72b0

Browse files
committed
Merge pull request #50321 from fatkodima/fix-polymorphic-belongs_to-with-query_constraints
Fix polymorphic `belongs_to` to correctly use parent's `query_constraints`
1 parent da2615b commit 59e72b0

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

activerecord/lib/active_record/reflection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ def association_class
870870
def association_primary_key(klass = nil)
871871
if primary_key = options[:primary_key]
872872
@association_primary_key ||= -primary_key.to_s
873-
elsif !polymorphic? && ((klass || self.klass).has_query_constraints? || options[:query_constraints])
873+
elsif (klass || self.klass).has_query_constraints? || options[:query_constraints]
874874
(klass || self.klass).composite_query_constraints_list
875875
elsif (klass || self.klass).composite_primary_key?
876876
# If klass has composite primary key of shape [:<tenant_key>, :id], infer primary_key as :id

activerecord/test/cases/associations_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,14 @@ def test_belongs_to_association_does_not_use_parent_query_constraints_if_not_con
284284
assert_equal(blog_post, comment.blog_post_by_id)
285285
end
286286

287+
def test_polymorphic_belongs_to_uses_parent_query_constraints
288+
parent_post = sharded_blog_posts(:great_post_blog_one)
289+
child_post = Sharded::BlogPost.create!(title: "Child post", blog_id: parent_post.blog_id, parent: parent_post)
290+
child_post.reload # reload to forget the parent association
291+
292+
assert_equal parent_post, child_post.parent
293+
end
294+
287295
def test_preloads_model_with_query_constraints_by_explicitly_configured_fk_and_pk
288296
comment = sharded_comments(:great_comment_blog_post_one)
289297
comments = Sharded::Comment.where(id: comment.id).preload(:blog_post_by_id).to_a

activerecord/test/models/sharded/blog_post.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ class BlogPost < ActiveRecord::Base
55
self.table_name = :sharded_blog_posts
66
query_constraints :blog_id, :id
77

8+
belongs_to :parent, class_name: name, polymorphic: true
89
belongs_to :blog
910
has_many :comments
1011
has_many :delete_comments, class_name: "Sharded::Comment", dependent: :delete_all
12+
has_many :children, class_name: name, as: :parent
1113

1214
has_many :blog_post_tags
1315
has_many :tags, through: :blog_post_tags

activerecord/test/schema/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@
313313

314314
create_table :sharded_blog_posts, force: true do |t|
315315
t.string :title
316+
t.references :parent, polymorphic: true
316317
t.integer :blog_id
317318
t.integer :revision
318319
end

0 commit comments

Comments
 (0)