ActiveRecord Query for many to many association
To produce the set of records only in cars, but not in drivers, you perform a left outer join, then exclude the records you don't want from the right side via a where clause.

In Rails 6 you do this in this way:

Car.left_outer_joins(:drivers).where(car_drivers: { id: nil })


Take a look at this article: A Visual Explanation of SQL Joins. It may help you figure out how to do some queries.

Aside from this, a good way to iterate over an array to select some elements according to a condition is to use the select method. Using select your code would be like this:

result = Car.all.select { |car| car.drivers.empty? }


But that is not the way to go here, I'm just showing how to use select.

Also, if you want only the id from the cars, you can add pluck(:id) at the end of the query. This is going to get only the id field from the database. Or you just use the method ids but the pluck method works for any field.