mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Implement count limit/offset support for has_many associations
[#348 state:resolved] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
This commit is contained in:
parent
13671cc565
commit
96c6fe0842
2 changed files with 16 additions and 1 deletions
|
@ -14,7 +14,16 @@ module ActiveRecord
|
|||
@finder_sql + " AND (#{sanitize_sql(options[:conditions])})"
|
||||
options[:include] ||= @reflection.options[:include]
|
||||
|
||||
@reflection.klass.count(column_name, options)
|
||||
value = @reflection.klass.count(column_name, options)
|
||||
|
||||
limit = @reflection.options[:limit]
|
||||
offset = @reflection.options[:offset]
|
||||
|
||||
if limit || offset
|
||||
[ [value - offset.to_i, 0].max, limit.to_i ].min
|
||||
else
|
||||
value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -48,6 +48,12 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
|
|||
assert_equal 2, Firm.find(:first).plain_clients.count(:name)
|
||||
end
|
||||
|
||||
def test_counting_with_association_limit
|
||||
firm = companies(:first_firm)
|
||||
assert_equal firm.limited_clients.length, firm.limited_clients.size
|
||||
assert_equal firm.limited_clients.length, firm.limited_clients.count
|
||||
end
|
||||
|
||||
def test_finding
|
||||
assert_equal 2, Firm.find(:first).clients.length
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue