From 9fa3f102813eeeec440abd75870dfa7b23835665 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Sat, 15 Sep 2012 12:31:53 +0400 Subject: Don't preserve SELECT columns on COUNT The COUNT clause of a finder_sql relationship is being rewritten from COUNT(*) to COUNT(table_name.*). This does not appear to be valid syntax in MySQL: ``` mysql> SELECT COUNT( table_name.* ) FROM `table_name`; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* ) FROM `table_name`' at line 1 ``` This fixes the bug, as well as adding tests so we don't re-introduce it in the future. Fixes #3956. --- activerecord/lib/active_record/associations/collection_association.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index b15df4f308..babbb12e99 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -373,7 +373,8 @@ module ActiveRecord # replace the SELECT clause with COUNT(SELECTS), preserving any hints within /* ... */ interpolate(options[:finder_sql]).sub(/SELECT\b(\/\*.*?\*\/ )?(.*)\bFROM\b/im) do count_with = $2.to_s - count_with = '*' if count_with.blank? || count_with =~ /,/ + p count_with + count_with = '*' if count_with.blank? || count_with =~ /,/ || count_with =~ /\.\*/ "SELECT #{$1}COUNT(#{count_with}) FROM" end end -- cgit v1.2.3