From 563704080cb3c2f21d1b5ecdc809fdd994db271c Mon Sep 17 00:00:00 2001 From: Alexandre Ruban Date: Wed, 6 Oct 2021 12:30:29 +0200 Subject: [PATCH] Fix joining through a polymorphic association --- activerecord/lib/active_record/reflection.rb | 2 +- .../cases/associations/has_many_associations_test.rb | 10 +++++++++- activerecord/test/models/category.rb | 3 +++ activerecord/test/models/essay.rb | 3 +++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 9410de8cb1..1b8e516a82 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -1027,7 +1027,7 @@ module ActiveRecord end def join_scopes(table, predicate_builder, klass = self.klass, record = nil) # :nodoc: - scopes = @previous_reflection.join_scopes(table, predicate_builder, record) + super + scopes = @previous_reflection.join_scopes(table, predicate_builder, klass, record) + super scopes << build_scope(table, predicate_builder, klass).instance_exec(record, &source_type_scope) end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 72ca063b57..51a1d18dff 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -116,7 +116,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase fixtures :accounts, :categories, :companies, :developers, :projects, :developers_projects, :topics, :authors, :author_addresses, :comments, :posts, :readers, :taggings, :cars, :tags, - :categorizations, :zines, :interests + :categorizations, :zines, :interests, :humans def setup Client.destroyed_client_ids.clear @@ -2537,6 +2537,14 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal post, image.imageable end + def test_joining_through_a_polymorphic_association_with_a_where_clause + writer = humans(:gordon) + category = categories(:general) + TypedEssay.create! category: category, writer: writer + + assert_equal 1, Category.joins(:human_writers_of_typed_essays).count + end + def test_build_with_polymorphic_has_many_does_not_allow_to_override_type_and_id welcome = posts(:welcome) tagging = welcome.taggings.build(taggable_id: 99, taggable_type: "ShouldNotChange") diff --git a/activerecord/test/models/category.rb b/activerecord/test/models/category.rb index 4672527271..fcdf8b6d31 100644 --- a/activerecord/test/models/category.rb +++ b/activerecord/test/models/category.rb @@ -30,6 +30,9 @@ class Category < ActiveRecord::Base has_many :authors, through: :categorizations has_many :authors_with_select, -> { select "authors.*, categorizations.post_id" }, through: :categorizations, source: :author + has_many :essays, primary_key: :name + has_many :human_writers_of_typed_essays, -> { where(essays: { type: TypedEssay.name }) }, through: :essays, source: :writer, source_type: "Human", primary_key: :name + scope :general, -> { where(name: "General") } # Should be delegated `ast` and `locked` to `arel`. diff --git a/activerecord/test/models/essay.rb b/activerecord/test/models/essay.rb index 06764250de..9fa2cb1a53 100644 --- a/activerecord/test/models/essay.rb +++ b/activerecord/test/models/essay.rb @@ -6,3 +6,6 @@ class Essay < ActiveRecord::Base belongs_to :category, primary_key: :name has_one :owner, primary_key: :name end + +class TypedEssay < Essay +end