Counting distinct or unique records
Closed this issue · 2 comments
wynksaiddestroy commented
As an example use the three classes from the documentation: User
, Group
and Membership
. Now assume that a user can have multiple memberships for a group.
Is there a way to save the distinct or unique user count for a group?
So I don't want this:
group.users.size
… but:
group.users.distinct.size
wynksaiddestroy commented
If I change the user model as follows:
class User < ActiveRecord::Base
has_many :group_memberships
has_many :groups, -> { distinct }, through: :group_memberships
end
… i won't have to use distinct
anymore. But my counter on the membership model:
counter_culture :group, column_name: "members_count"
won't count the distinct users but all.
magnusvk commented
This isn't possible with counter_culture as far as I know. If you had a pull request that added support for this, I'd be happy to take a look at it though!