Group By not working after upgrading to 4

I have a very old site that was built on Middleman 3.x that I’m trying to upgrade to the latest version (4.x) and get it deployed to Vercel. I have worked through several issues to get this running. At the moment the only issue I can’t figure out is with a group_by call to list a distinct list of categories with a count of articles within. See the code below:

> <% categories = blog.articles.group_by {|a| a.metadata[:page]['category']}.sort_by {|a| a} %>
>             <% categories.each do |category, articles| %>
>             <!-- BEGIN - Category item -->
>             <li>
>                 <%= link_to category, category_path(category) %>
>                 <span class="widget-hint"><%= articles.size %> Posts</span>
>             </li>
>             <!-- END - Category item -->
>             <% end %>

Appreciate any assistance with reworking this so that the functionality runs as expected.

Thanks in advance!

-K

In case someone comes in here with a similar issue, here is what I was able to make work:

<% categories = blog.articles.group_by {|a| a.data.category}.sort_by {|a| a} %>
            <% categories.each do |category, articles| %>
            <!-- BEGIN - Category item -->
            <li>
                <%= link_to category, category_path(category) %>
                <span class="widget-hint"><%= articles.size %> Posts</span>
            </li>
            <!-- END - Category item -->
            <% end %>