Skip to content

Instantly share code, notes, and snippets.

@zaiste
Last active May 3, 2018 23:53
Show Gist options
  • Save zaiste/0674cb5aee97bb9322b161a7dae4a9bb to your computer and use it in GitHub Desktop.
Save zaiste/0674cb5aee97bb9322b161a7dae4a9bb to your computer and use it in GitHub Desktop.
Rails: Find Column Null, not Null or Empty

Let’s find records where column is null or empty using Active Record

Article.where(topic: [nil, ""])

Let’s now find records where column is NOT null or empty using Active Record

Article.where.not(topic: [nil, ""])

This Active Record expression will be converted to the following SQL query.

SELECT "articles.*" 
FROM "articles"
WHERE 
NOT (("articles"."topic" = '' OR "articles"."topic" IS NULL))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment