diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_association.rb | 11 | ||||
-rwxr-xr-x | activerecord/test/associations_test.rb | 4 |
3 files changed, 17 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 59bcb3e712..af797707b0 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added HasManyAssociation#count that works like Base#count #413 [intinig] + * Fixed handling of binary content in blobs and similar fields for Ruby/MySQL and SQLite #409 [xal] * Added dynamic attribute-based finders as a cleaner way of getting objects by simple queries without turning to SQL. 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 diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index f94f04e279..844f075f34 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -149,6 +149,10 @@ class HasManyAssociationsTest < Test::Unit::TestCase @signals37.clients_of_firm.each {|f| } end + def test_counting + assert_equal 2, Firm.find_first.clients.count + end + def test_finding assert_equal 2, Firm.find_first.clients.length end |