diff options
author | Emilio Tagua <miloops@gmail.com> | 2009-12-30 01:12:38 +0530 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-12-30 01:12:38 +0530 |
commit | 00cd3789f6d53163229669ad82a5d87fcdcb49ba (patch) | |
tree | bb5bba7ee8ce3f492c169da56f112c8767b9acf8 /activerecord/lib/active_record | |
parent | aa1b32ae9774e3f2a95faa0ec9efdf295958106d (diff) | |
download | rails-00cd3789f6d53163229669ad82a5d87fcdcb49ba.tar.gz rails-00cd3789f6d53163229669ad82a5d87fcdcb49ba.tar.bz2 rails-00cd3789f6d53163229669ad82a5d87fcdcb49ba.zip |
Relation#count should look for projections in chained relations and perform the count on the given column
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/relational_calculations.rb | 19 |
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 |