postgresqlactiverecordruby-on-rails-5jsonb

How do I query for empty jsonb arrays in Postgres, Activerecord, Rails 5


I have a Post model with the column defined by the following migration:

add_column :posts, :comments, :jsonb, default: []

add_index :posts, :comments, using: :gin

I would like to know the query to run so that I have a count of all the Posts which have the default, empty array as comments.


Solution

  • Post.where("comments = '[]'").count