diff options
author | Ernie Miller <ernie@metautonomo.us> | 2008-08-28 14:01:42 -0400 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-08-28 11:58:25 -0700 |
commit | 44af2efa2c7391681968c827ca47201a0a02e974 (patch) | |
tree | a25ede2a88799c281468fcdaa4321d6048cee240 /activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb | |
parent | ce4d13861dc54a1ac7fbe411327b9a2427f95366 (diff) | |
download | rails-44af2efa2c7391681968c827ca47201a0a02e974.tar.gz rails-44af2efa2c7391681968c827ca47201a0a02e974.tar.bz2 rails-44af2efa2c7391681968c827ca47201a0a02e974.zip |
Refactored AssociationCollection#count for uniformity and Ruby 1.8.7 support.
[#831 state:resolved]
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb')
-rw-r--r-- | activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index e7e433b6b6..3d689098b5 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -78,6 +78,16 @@ module ActiveRecord end @join_sql = "INNER JOIN #{@owner.connection.quote_table_name @reflection.options[:join_table]} ON #{@reflection.quoted_table_name}.#{@reflection.klass.primary_key} = #{@owner.connection.quote_table_name @reflection.options[:join_table]}.#{@reflection.association_foreign_key}" + + 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 (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + else + @counter_sql = @finder_sql + end end def construct_scope |