HHH v4

find all records that have empty / no associations (left_outer_joins)

Edit
equivalent Web Development
Public
Rails
AREL trick
let say we have M:M  relation

  • maps
  • maps_strategies
  • maps_strategy_maps  (join table)

and you want to select items that are not joined in that other table (Maps that are not assigned to any MapStrategy)

@maps = Map.all

# just Maps with no strategy
@maps_without_strategy = @maps.left_outer_joins(:map_strategies).where(map_strategies: {id: nil})

Or you can do 

@maps_without_strategy = @maps.where.not(id: MapStrategyMap.select("map_id"))