Rails: Find the "has_one" record that doesn't have one

I'm not sure what the Ruby code would be, but I think the SQL should be something like:

SELECT * FROM Foo WHERE id NOT IN (SELECT foo_id FROM Bar)


Another way (without using SQL) would be to do something like:

Foo.all.select{ |f| !f.bar }

This would return an array of Foo objects which don't have a related Bar object.

In this method, you're not relying on specific table information. If the foreign_key column were to change in the future Foo -> Bar association, this method would continue to work.