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:
Tarmo Tänav 2008-08-26 19:29:16 +03:00 committed by Jeremy Kemper
parent 13671cc565
commit 96c6fe0842
2 changed files with 16 additions and 1 deletions

@ -14,7 +14,16 @@ def count(*args)
@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 @@ def test_counting_with_column_name_and_hash
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