This project is archived and is in readonly mode.

#348 ✓resolved
Bernardo Padua

Count on has_many associations doesn't respect the association :limit and :offset options

Reported by Bernardo Padua | June 5th, 2008 @ 11:11 PM | in 2.1.1

If I have:

Class Post < AR:Base
  has_many :comments, :limit=> 2

And run a count or size in the association, it won't respect the :limit (as well as :offset) options, like find will.

Say we have 3 comments:

post.comments.count # => 3 
# Sql:
# SELECT count(*) AS count_all FROM "comments" WHERE
# ("comments".post_id = 271642539)

post.comments.length  # => 2
# Sql:
# SELECT * AS count_all FROM "comments" WHERE
# ("comments".post_id = 271642539) LIMIT 2

To run the attached test that fails use:

rake test_sqlite3 TEST=test/cases/associations/has_many_associations_test.rb

Tested on Edge, 2.1, Sqlite3 and Postgres.

#302 by Ryan Bates describes another discrepancy of count vs. find. Maybe it's time for a refactoring count so it uses the same infrastructure to generate the queries as find?

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>