Skip to content

Instantly share code, notes, and snippets.

@goooooouwa
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goooooouwa/d7d02263b2109869471c to your computer and use it in GitHub Desktop.
Save goooooouwa/d7d02263b2109869471c to your computer and use it in GitHub Desktop.
active record query to find all companies with no contact
# in English: return all companies that have a contact whose id is nil.
# why this will work is because the "includes" method will join the two tables( companies and contacts) into one table.
# for those companies who has no contacts, the record's contact is null.
Company.includes(:contacts).where(contacts: {id: nil})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment