aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-01-02 18:44:55 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-01-02 18:44:55 +0000
commit609ca177fa66ab0fbe19dd0cdcd7d8f1576cd9da (patch)
treeae898c43f99aaa588256152d31156176288611b4 /activerecord/lib
parent375568b7cb476285a9f8ea2c748d4987a477a119 (diff)
downloadrails-609ca177fa66ab0fbe19dd0cdcd7d8f1576cd9da.tar.gz
rails-609ca177fa66ab0fbe19dd0cdcd7d8f1576cd9da.tar.bz2
rails-609ca177fa66ab0fbe19dd0cdcd7d8f1576cd9da.zip
Added HasManyAssociation#count that works like Base#count #413 [intinig]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@318 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/associations/has_many_association.rb11
1 files changed, 11 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb
index 665d28869f..384433e29a 100644
--- a/activerecord/lib/active_record/associations/has_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_association.rb
@@ -47,6 +47,17 @@ module ActiveRecord
end
end
+ # Count the number of associated records. All arguments are optional.
+ def count(runtime_conditions = nil)
+ if @options[:finder_sql]
+ @association_class.count_by_sql(@finder_sql)
+ else
+ sql = @finder_sql
+ sql << " AND #{sanitize_sql(runtime_conditions)}" if runtime_conditions
+ @association_class.count(sql)
+ end
+ end
+
# Find the first associated record. All arguments are optional.
def find_first(conditions = nil, orderings = nil)
find_all(conditions, orderings, 1).first