Skip to content

Commit 967c33d

Browse files
committed
Merge pull request rails#51507 from joshuay03/fix-polymorphic-has-many-through-with-association-scope
[Fix rails#40109] Incorrect joins for polymorphic `has_many through:` with association scope
2 parents 8ceb7b9 + 0d012fa commit 967c33d

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Fix association scopes applying on the incorrect join when using a polymorphic `has_many through:`.
2+
3+
*Joshua Young*
4+
15
* Allow `ActiveRecord::Base#pluck` to accept hash arguments with symbol and string values.
26

37
```ruby

activerecord/lib/active_record/reflection.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,10 @@ def initialize(reflection, previous_reflection)
12361236
end
12371237

12381238
def join_scopes(table, predicate_builder = nil, klass = self.klass, record = nil) # :nodoc:
1239-
scopes = @previous_reflection.join_scopes(table, predicate_builder, klass, record) + super
1239+
scopes = super
1240+
unless @previous_reflection.through_reflection?
1241+
scopes += @previous_reflection.join_scopes(table, predicate_builder, klass, record)
1242+
end
12401243
scopes << build_scope(table, predicate_builder, klass).instance_exec(record, &source_type_scope)
12411244
end
12421245

activerecord/test/cases/associations/nested_through_associations_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,19 @@ def test_polymorphic_has_many_through_joined_different_table_twice
639639
assert_equal hotel, Hotel.joins(:cake_designers, :drink_designers).take
640640
end
641641

642+
def test_has_many_through_polymorphic_with_scope
643+
Post.delete_all
644+
645+
post = Post.create!(title: "Catchy Title", body: "Interesting body.")
646+
category = Category.create!(name: "Anything")
647+
Post::CategoryPost.create!(post: post, category: category)
648+
649+
author = authors(:bob)
650+
Essay.create!(writer: author, category: category)
651+
652+
assert_equal 1, Post.joins(:authors_of_essays_named_bob).count
653+
end
654+
642655
def test_has_many_through_reset_source_reflection_after_loading_is_complete
643656
preloaded = Category.preload(:ordered_post_comments).find(1, 2).last
644657
original = Category.find(2)

activerecord/test/models/post.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ def greeting
124124
has_and_belongs_to_many :categories
125125
has_and_belongs_to_many :special_categories, join_table: "categories_posts", association_foreign_key: "category_id"
126126

127+
has_many :essays, through: :categories
128+
has_many :authors_of_essays_named_bob,
129+
-> { where(name: "Bob") },
130+
through: :essays,
131+
source: :writer,
132+
source_type: "Author"
133+
127134
has_many :taggings, as: :taggable, counter_cache: :tags_count
128135
has_many :tags, through: :taggings do
129136
def add_joins_and_select

0 commit comments

Comments
 (0)