Search code examples
ruby-on-railsrubypostgresqlactiverecordruby-on-rails-5

ActiveRecord: Find where column is nil or empty array


I have an array column in postgres and I want to find all the records where this column is either nil or []. I can search one or the other fine but trying both fails.

This is what I attempted

Model.where(column: [nil, []])

But I get the error

ActiveRecord::StatementInvalid (PG::InvalidTextRepresentation: ERROR:  malformed array literal: "{NULL,{}}")
DETAIL:  Unexpected "{" character.

Solution

  • This worked for me

    Model.where('column IS NULL OR column = ?', '{}')