Skip to content
This repository
Browse code

Fixed that HasManyAssociation#count was using :finder_sql rather than…

… :counter_sql if it was available #445 [Scott Barron]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@834 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 5e44eda7872c6ee33e0d09a4b9fd78896e14b95a 1 parent dfac1ce
David Heinemeier Hansson authored March 06, 2005
2  activerecord/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Fixed that HasManyAssociation#count was using :finder_sql rather than :counter_sql if it was available #445 [Scott Barron]
  4
+
3 5
 * Added better defaults for composed_of, so statements like composed_of :time_zone, :mapping => %w( time_zone time_zone ) can be written without the mapping part (it's now assumed)
4 6
 
5 7
 * Added MacroReflection#macro which will return a symbol describing the macro used (like :composed_of or :has_many) #718, #248 [james@slashetc.com]
4  activerecord/lib/active_record/associations/has_many_association.rb
@@ -33,7 +33,9 @@ def find_all(runtime_conditions = nil, orderings = nil, limit = nil, joins = nil
33 33
 
34 34
       # Count the number of associated records. All arguments are optional.
35 35
       def count(runtime_conditions = nil)
36  
-        if @options[:finder_sql]
  36
+        if @options[:counter_sql]
  37
+          @association_class.count_by_sql(@counter_sql)
  38
+        elsif @options[:finder_sql]
37 39
           @association_class.count_by_sql(@finder_sql)
38 40
         else
39 41
           sql = @finder_sql
2  activerecord/test/fixtures/company.rb
@@ -6,7 +6,7 @@ class Company < ActiveRecord::Base
6 6
 
7 7
 
8 8
 class Firm < Company
9  
-  has_many :clients, :order => "id", :dependent => true
  9
+  has_many :clients, :order => "id", :dependent => true, :counter_sql => "SELECT COUNT(*) FROM companies WHERE firm_id = 1 AND (type = 'Client' OR type = 'SpecialClient' OR type = 'VerySpecialClient' )"
10 10
   has_many :clients_sorted_desc, :class_name => "Client", :order => "id DESC"
11 11
   has_many :clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id"
12 12
   has_many :clients_like_ms, :conditions => "name = 'Microsoft'", :class_name => "Client", :order => "id"

0 notes on commit 5e44eda

Please sign in to comment.
Something went wrong with that request. Please try again.