aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/association_collection.rb
diff options
context:
space:
mode:
authorPatrick Joyce <patrick.t.joyce@gmail.com>2009-06-21 22:07:23 +0100
committerPratik Naik <pratiknaik@gmail.com>2009-06-21 22:07:23 +0100
commit4851ca9e13a4317342df02ae25b1929340523f7a (patch)
tree790e327cce1077624b23db92cf56579e10bef59e /activerecord/lib/active_record/associations/association_collection.rb
parenteb30e4ca40370b416566f1bbaf0204bb379883e4 (diff)
downloadrails-4851ca9e13a4317342df02ae25b1929340523f7a.tar.gz
rails-4851ca9e13a4317342df02ae25b1929340523f7a.tar.bz2
rails-4851ca9e13a4317342df02ae25b1929340523f7a.zip
Generate proper :counter_sql from :finder_sql when there is a newline character immediately following 'SELECT' [#2118 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activerecord/lib/active_record/associations/association_collection.rb')
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb14
1 files changed, 13 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index e12f6be35d..84edaec15e 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -351,7 +351,19 @@ module ActiveRecord
protected
def construct_find_options!(options)
end
-
+
+ def construct_counter_sql
+ if @reflection.options[:counter_sql]
+ @counter_sql = interpolate_sql(@reflection.options[:counter_sql])
+ elsif @reflection.options[:finder_sql]
+ # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */
+ @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT\b(\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" }
+ @counter_sql = interpolate_sql(@reflection.options[:counter_sql])
+ else
+ @counter_sql = @finder_sql
+ end
+ end
+
def load_target
if !@owner.new_record? || foreign_key_present
begin