File tree Expand file tree Collapse file tree 4 files changed +12
-1
lines changed
Expand file tree Collapse file tree 4 files changed +12
-1
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments