Skip to content

Commit

Permalink
Merge PR #43389
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Dec 20, 2021
2 parents db56042 + 5637040 commit f8f8d97
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/reflection.rb
Expand Up @@ -1031,7 +1031,7 @@ def initialize(reflection, previous_reflection)
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

Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -2555,6 +2555,14 @@ def test_with_polymorphic_has_many_with_custom_columns_name
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")
Expand Down
3 changes: 3 additions & 0 deletions activerecord/test/models/category.rb
Expand Up @@ -30,6 +30,9 @@ def self.what_are_you
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`.
Expand Down
2 changes: 2 additions & 0 deletions activerecord/test/models/essay.rb
Expand Up @@ -9,3 +9,5 @@ class Essay < ActiveRecord::Base

class EssaySpecial < Essay
end
class TypedEssay < Essay
end

0 comments on commit f8f8d97

Please sign in to comment.