Reputation: 1799
i am trying to use the rails helper method uniq
but unsure why it is not working. Your help would be much appreciated
@forms.count
= 3
@forms.female_applicants.count
= 2
@forms.female_applicants.uniq.count
= gives me 2
but the answer should be 1
Model
Form
belongs to a user
User
has many forms
User
belongs to category_gender
Category_gender
has many users
Form.rb
scope :female_applicants, ->() { joins(:category_gender).where('category_genders.name' => "Female") }
My Question: If the same woman has 2 forms, could one kindly advise me how to display the number of forms uniquely as 1 rather than 2. so when queried
@forms.female_applicants.uniq.count
the result will be1
and not2
if there is a better way than using theuniq
method i would very much appreciate if told
Upvotes: 2
Views: 266
Reputation: 1799
@Dan & @hypern you were right - there were non-unique elements present which was why i could not use the rails method uniq
using @forms.female_applicants.group_by(&:user).count
resulted in the answer i was looking for
many thanks
Upvotes: 1