aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relational_calculations.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/relational_calculations.rb')
-rw-r--r--activerecord/lib/active_record/relational_calculations.rb19
1 files changed, 17 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/relational_calculations.rb b/activerecord/lib/active_record/relational_calculations.rb
index d77624c7bf..5e5ea9e0c1 100644
--- a/activerecord/lib/active_record/relational_calculations.rb
+++ b/activerecord/lib/active_record/relational_calculations.rb
@@ -109,11 +109,11 @@ module ActiveRecord
# TODO : relation.projections only works when .select() was last in the chain. Fix it!
case args.size
when 0
- select = @relation.send(:select_clauses).join(', ') if @relation.respond_to?(:projections) && @relation.projections.present?
+ select = get_projection_name_from_chained_relations
column_name = select if select !~ /(,|\*)/
when 1
if args[0].is_a?(Hash)
- select = @relation.send(:select_clauses).join(', ') if @relation.respond_to?(:projections) && @relation.projections.present?
+ select = get_projection_name_from_chained_relations
column_name = select if select !~ /(,|\*)/
options = args[0]
else
@@ -165,5 +165,20 @@ module ActiveRecord
column ? column.type_cast(value) : value
end
+ def get_projection_name_from_chained_relations
+ name = nil
+ if @relation.respond_to?(:projections) && @relation.projections.present?
+ name = @relation.send(:select_clauses).join(', ')
+ elsif @relation.respond_to?(:relation) && relation = @relation.relation
+ while relation.respond_to?(:relation)
+ if relation.respond_to?(:projections) && relation.projections.present?
+ name = relation.send(:select_clauses).join(', ')
+ end
+ relation = relation.relation
+ end
+ end
+ name
+ end
+
end
end