I have an ActiveRecord model let's call it `API::Response` and within that we have column named `error_messages` which stores the error messages when the API call fails.

The structure of `API::Response` is something like:

```rb
#<API::Response:0x0000000019579298
 id: "fffefdae43c0",
 api_id: "11c91374f10e",
 parent_id: nil,
 code: "ABC123",
 meta_field: "{}",
 lock_version: 1,
 created_at: Thu, 01 Feb 2019 15:28:37 UTC +00:00,
 updated_at: Tue, 09 Oct 2019 20:32:03 UTC +00:00,
 version: 1538352000,
 error_messages: {"api_failure_1"=> "API failure reason 1", "api_failure_2"=> "API failure reason 2"},
 output_data: { "foo" => "bar"},
 override_applied: false>

```

What I want to do is to get a result where I am grouping by `keys` within the `error_messages` column and get the count of occurrence each key:

so some something like:

```
key                   | count
-----------------------------
api_failure_1         |  1
api_failure_2         |  3
some_other_failure    |  n
...

```

where `keys` in the `error_messages` jsonb field are dynamic.

I tried to do something like

```
API::Response.where.not("error_messages = '{}'").group("error_messages").count

```

but this just gives me count of each distinct `error_messages` 

```rb
=> {
 {"api_failure_1"=> "API failure 1",
  "api_failure_2"=> "API failure 2"}=>1,
 {"api_failure_1"=> "API failure 1",
  "api_failure_3"=> "API failure 3"}=>1
}

```

rather then the count of each key within.

I am on Rails 5.2 and PostgreSQL 10.4

Any pointers or help is very much appreciated.