From 00cd3789f6d53163229669ad82a5d87fcdcb49ba Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Wed, 30 Dec 2009 01:12:38 +0530 Subject: Relation#count should look for projections in chained relations and perform the count on the given column Signed-off-by: Pratik Naik --- .../lib/active_record/relational_calculations.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'activerecord/lib') 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 -- cgit v1.2.3