sql - AND multiple JOIN statements in Rails -
i have table users , roles. i'm using has many through relationship. trying create query find users have of roles in array.
ex.
role_ids = [2, 4, 6] user.filter(role_ids) return users have roles ids 2, 4, 6.
this have far.
def self.filter(role_ids) results = user.joins(:roles).where(roles: {id: role_ids} ) end
the problem statement returns users have @ least 1 of roles in role_ids.
how make statement give me intersection, not union?
i think asking unique instances of users meet role filter criteria. if so, should work.
def self.filter(role_ids) results = user.joins(:roles).where(roles: {id: role_ids} ).uniq end
Comments
Post a Comment