Skip to content

Commit f12ff8d

Browse files
Merge pull request rails#19452 from pinglamb/fix-referencing-wrong-alias-when-joining-tables-of-has-many-through-association
Fix referencing wrong aliases while joining tables of has many through association
1 parent 94249d4 commit f12ff8d

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* Fix referencing wrong table aliases while joining tables of has many through
2+
association (only when calling calculation methods).
3+
4+
Fixes #19276.
5+
6+
*pinglamb*
7+
18
* Don't attempt to update counter caches, when the column wasn't selected.
29

310
Fixes #19437.

activerecord/lib/active_record/relation/finder_methods.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def construct_join_dependency(joins = [])
379379
def construct_relation_for_association_calculations
380380
from = arel.froms.first
381381
if Arel::Table === from
382-
apply_join_dependency(self, construct_join_dependency)
382+
apply_join_dependency(self, construct_join_dependency(joins_values))
383383
else
384384
# FIXME: as far as I can tell, `from` will always be an Arel::Table.
385385
# There are no tests that test this branch, but presumably it's

activerecord/test/cases/calculations_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
require 'models/speedometer'
1212
require 'models/ship_part'
1313
require 'models/treasure'
14+
require 'models/developer'
15+
require 'models/comment'
16+
require 'models/rating'
17+
require 'models/post'
1418

1519
class NumericData < ActiveRecord::Base
1620
self.table_name = 'numeric_data'
@@ -631,4 +635,11 @@ def test_grouped_calculation_with_polymorphic_relation
631635

632636
assert_equal({ "has trinket" => part.id }, ShipPart.joins(:trinkets).group("ship_parts.name").sum(:id))
633637
end
638+
639+
def test_should_reference_correct_aliases_while_joining_tables_of_has_many_through_association
640+
assert_nothing_raised ActiveRecord::StatementInvalid do
641+
developer = Developer.create!(name: 'developer')
642+
developer.ratings.includes(comment: :post).where(posts: { id: 1 }).count
643+
end
644+
end
634645
end

0 commit comments

Comments
 (0)