Rails: Find Column Null, not Null or Empty


2018-02-04 · 1 min read

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))