I have a has_many relationship of models Role and Access through model Permission.
I have a situation that no two roles should have identical accesses. So, I created a custom validation which basically results in an error message when current role(being created) is assigned the same accesses as previously existing roles using,
errors.add(:Role, "already exists with selected permissions") if Role.all.map(&:access_ids).include?(self.access_ids)
This all works fine. Now I need to fetch the role which has identical accesses as current role. So, how do I do that ? I tried with
Role.includes(:accesses).where(:accesses => {:id => [1,2]}).count
but this returns all roles whose access ids have either 1 or 2 (say [1, 2, 3, 4], [1], [2]) . What I need is to get the role whose access ids are exactly 1 and 2.
It would be nice if i could replace '=>' with '==' in above query :) But that obviously does not work.