I have User
has_many Badge
s. When I want to retrieve all users who has gold badge, I just write:
User.joins(:badges).where(:id => gold.id)
But how to retrieve all users who has gold badge AND silver badge? I tried thi:
User.joins(:badges).where(:id => gold.id).where(:id => silver.id)
Obviously I got nil
. What's the right way?
To clarify: I want
the users who have at least two badges: gold badge and silver badge
Or you can say:
the intersection of
'users who have gold badge' and 'users who have silver badge'
not
the users who have a badge which is gold and silver