Skip to content

Commit

Permalink
Added HasManyAssociation#count that works like Base#count #413 [intinig]
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@318 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Jan 2, 2005
1 parent 375568b commit 609ca17
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,7 @@
*SVN* *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] * 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. * Added dynamic attribute-based finders as a cleaner way of getting objects by simple queries without turning to SQL.
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ def find_all(runtime_conditions = nil, orderings = nil, limit = nil, joins = nil
end end
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. # Find the first associated record. All arguments are optional.
def find_first(conditions = nil, orderings = nil) def find_first(conditions = nil, orderings = nil)
find_all(conditions, orderings, 1).first find_all(conditions, orderings, 1).first
Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/associations_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ def force_signal37_to_load_all_clients_of_firm
@signals37.clients_of_firm.each {|f| } @signals37.clients_of_firm.each {|f| }
end end


def test_counting
assert_equal 2, Firm.find_first.clients.count
end

def test_finding def test_finding
assert_equal 2, Firm.find_first.clients.length assert_equal 2, Firm.find_first.clients.length
end end
Expand Down

0 comments on commit 609ca17

Please sign in to comment.