Permalink
Browse files

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
  • Loading branch information...
1 parent 375568b commit 609ca177fa66ab0fbe19dd0cdcd7d8f1576cd9da @dhh dhh committed Jan 2, 2005
View
@@ -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.
@@ -47,6 +47,17 @@ def find_all(runtime_conditions = nil, orderings = nil, limit = nil, joins = nil
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
@@ -149,6 +149,10 @@ def force_signal37_to_load_all_clients_of_firm
@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

0 comments on commit 609ca17

Please sign in to comment.