When joining with other tables, it is often wrong to use
.count() to get the number of records. For example when trying to
count the number of users who have posts:
User.scoped(:joins => :posts).count
Gives a count of all the posts, since it's counting rows
returned by the DB. However, the query was built on User, so one
would expect the count to be related to the number of Users. For
example these are equivalent:
User.scoped(:joins => :posts).count
Post.scoped(:joins => :user).count
This query does the right thing, which is counting the number of
users who have posts:
User.scoped(:joins => :posts).count(:id, :distinct => true)
So, it seems like .count() should maybe default to using "(:id,
:distinct => true)", with the option to say :distinct =>
false, etc, instead of the other way around.