How do I find where id does not match any of an array?
        Posted  
        
            by 
                Nick5a1
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Nick5a1
        
        
        
        Published on 2012-06-30T23:59:28Z
        Indexed on 
            2012/07/01
            9:15 UTC
        
        
        Read the original article
        Hit count: 299
        
ruby-on-rails
|ruby
I have a Workout model that has and belongs to many Equipment models. I have an array of some Equipment IDs. I want to find all Workouts that don't have any Equipment assigned that matches any of the array of Equipment IDs. 
So, if my array = [2,3,5] I want to find all workouts where the assigned equipment ids does not include 2, 3 or 5.
EDIT:
Workout.joins(:equipment).where("equipment.id not in(?)",[2,3,5]).uniq
Assuming five instances of Equipment, the code above returns workouts with equipment.ids 1 and 4 (good), but also returns partial matches for example Workouts with equipment.id = [1,2], [1,2,3].
© Stack Overflow or respective owner