Skip to content

Commit

Permalink
Implement count limit/offset support for has_many associations
Browse files Browse the repository at this point in the history
[#348 state:resolved]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
tarmo authored and jeremy committed Aug 28, 2008
1 parent 0ed29df commit 367942d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Expand Up @@ -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

Expand Down
Expand Up @@ -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
Expand Down

0 comments on commit 367942d

Please sign in to comment.