Search code examples
ruby-on-railspostgresqlactiverecordruby-on-rails-5jsonb

Return activerecord results for jsonb column array count greater than given value


In my rails 5 application with a Postgresql database, there is a class Listing with a jsonb column media.

I want to return the records that have array length greater than 10 in the media column.

How would I construct that query in ActiveRecord?


Solution

  • Turns out it is very straightforward

    Listing.where('JSONB_ARRAY_LENGTH(media) > 10')