diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2011-11-03 21:14:51 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2011-11-03 21:17:15 -0700 |
commit | 533a9f84b035756eedf9fdccf0c494dc9701ba72 (patch) | |
tree | 5454ceae355dc24e742f93f6fe81a905b3da15d9 /activerecord/lib/active_record | |
parent | b5f908a7ad515215f164e306f82c3e6c506182cb (diff) | |
download | rails-533a9f84b035756eedf9fdccf0c494dc9701ba72.tar.gz rails-533a9f84b035756eedf9fdccf0c494dc9701ba72.tar.bz2 rails-533a9f84b035756eedf9fdccf0c494dc9701ba72.zip |
Merge pull request #3507 from jmazzi/issue-3503
Preserve SELECT columns on the COUNT for finder_sql when possible
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index cec876149c..362f1053cd 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -344,8 +344,12 @@ module ActiveRecord if options[:counter_sql] interpolate(options[:counter_sql]) else - # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ - interpolate(options[:finder_sql]).sub(/SELECT\b(\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } + # 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 =~ /,/ + "SELECT #{$1}COUNT(#{count_with}) FROM" + end end end |