mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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
This commit is contained in:
parent
375568b7cb
commit
609ca177fa
3 changed files with 17 additions and 0 deletions
|
@ -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 @@ 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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue