One more approach would be to write some SQL
:
Property.joins("LEFT OUTER JOIN users ON users.property_id = properties.id").
where('users.id IS NULL').
uniq
The code above is being translated to the following pure SQL
query to the database:
SELECT DISTINCT properties.* FROM properties
LEFT OUTER JOIN users on users.property_id = properties.id
WHERE users.id IS NULL;
LEFT JOIN
keyword returns all rows from the left table (properties
), with the matching rows in the right table (users
). The result is NULL
in the right side when there is no match. Afterwards WHERE
keyword filters results by a condition that we're intrested in those rows which have NULL
on the right side only.

Reference: SQL LEFT JOIN Keyword